docparser.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. Trex.install("editor.getDocParser",
  2. function(editor, toolbar, sidebar, canvas, config){
  3. var _docparser = new Trex.Docparser(editor, sidebar, config);
  4. editor.getDocParser = function() {
  5. return _docparser;
  6. };
  7. }
  8. );
  9. Trex.Docparser =Trex.Class.create( {
  10. initialize : function(editor, sidebar, config){
  11. this.editor = editor;
  12. this.sidebar = sidebar;
  13. this.config = config;
  14. },
  15. filters: {},
  16. /**
  17. * register contents converting filter
  18. *
  19. * original = DB에 저장되는 컨텐츠
  20. * html = wysiwyg 모드에서 보이는 컨텐츠
  21. * source = source 모드에서 보이는 컨텐츠
  22. * text = text 모드에서 보이는 컨텐츠
  23. *
  24. * @example
  25. * editor.getDocParser().registerFilter(
  26. 'filter/converting', {
  27. 'text@load': function(contents){ // orginal -> text
  28. return contents;
  29. },
  30. 'source@load': function(contents){ // orginal -> source
  31. return contents;
  32. },
  33. 'html@load': function(contents){ // orginal -> html
  34. return contents;
  35. },
  36. 'text4save': function(contents){ // text -> orginal
  37. return contents;
  38. },
  39. 'source4save': function(contents){ // source -> orginal
  40. return contents;
  41. },
  42. 'html4save': function(contents){ // html -> orginal
  43. return contents;
  44. },
  45. 'text2source': function(contents){ // text -> source
  46. return contents;
  47. },
  48. 'text2html': function(contents){ // text -> html
  49. return contents;
  50. },
  51. 'source2text': function(contents){ // source -> text
  52. return contents;
  53. },
  54. 'source2html': function(contents){ // source -> html
  55. return contents;
  56. },
  57. 'html2text': function(contents){ // html -> text
  58. return contents;
  59. },
  60. 'html2source': function(contents){ // html -> source
  61. return contents;
  62. }
  63. }
  64. );
  65. */
  66. registerFilter: function(name, filter){
  67. this.filters[name] = filter;
  68. },
  69. getFilter: function(name){
  70. return this.filters[name];
  71. },
  72. executeFilters: function (cmd, contents) {
  73. var filters = this.filters;
  74. ["before " + cmd, cmd, "after " + cmd].each(function (cmd) {
  75. var name, filter;
  76. for (name in filters) {
  77. if (filters.hasOwnProperty(name)) {
  78. filter = filters[name];
  79. if (filter[cmd]) {
  80. contents = filter[cmd](contents);
  81. }
  82. }
  83. }
  84. });
  85. return contents;
  86. },
  87. getContentsAtChangingMode: function(contents, oldMode, newMode) {
  88. if (oldMode == newMode) {
  89. return contents;
  90. }
  91. contents = contents.trim() || "";
  92. return this.executeFilters(oldMode.concat("2").concat(newMode), contents);
  93. },
  94. convertAtLoad: function(contents, editorMode, inputMode) { // For Display
  95. /*
  96. * DB에 저장된 컨텐츠
  97. * > original, text
  98. */
  99. if(inputMode == 'original') { //original 컨텐츠 변환
  100. contents = this.executeFilters(editorMode.concat('@load'), contents);
  101. } else { //그외 모드, 자동저장은 변환없이 저장됨.
  102. if(editorMode != inputMode) {
  103. contents = this.executeFilters(inputMode.concat("2").concat(editorMode), contents);
  104. }
  105. }
  106. return contents;
  107. },
  108. convertAtSave: function(contents, editorMode, outputMode) { // For Save
  109. if (outputMode == 'original') { //original 컨텐츠 변환
  110. contents = this.executeFilters(editorMode.concat('4save'), contents);
  111. } else { //그외 모드, 자동저장은 변환없이 저장됨.
  112. if (editorMode != outputMode) {
  113. contents = this.executeFilters(editorMode.concat("2").concat(outputMode), contents);
  114. }
  115. }
  116. return contents;
  117. },
  118. /* 외부에서 참조할 컨텐츠 변환 필터명 시작 */
  119. text2source: function(contents) {
  120. return this.executeFilters("text2source", contents);
  121. },
  122. text2html: function(contents) {
  123. if (contents === "") {
  124. return $tom.EMPTY_PARAGRAPH_HTML;
  125. }
  126. return this.executeFilters("text2html", contents);
  127. },
  128. source2text: function(contents) {
  129. return this.executeFilters("source2text", contents);
  130. },
  131. source2html: function(contents) {
  132. if (contents === "") {
  133. return $tom.EMPTY_PARAGRAPH_HTML;
  134. }
  135. return this.executeFilters("source2html", contents);
  136. },
  137. html2text: function(contents) {
  138. return this.executeFilters("html2text", contents);
  139. },
  140. html2source: function(contents) {
  141. return this.executeFilters("html2source", contents);
  142. }
  143. /* 외부에서 참조할 컨텐츠 변환 필터명 끝 */
  144. }
  145. );