plugin.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.2.0 (2020-02-13)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  13. var getNonEditableClass = function (editor) {
  14. return editor.getParam('noneditable_noneditable_class', 'mceNonEditable');
  15. };
  16. var getEditableClass = function (editor) {
  17. return editor.getParam('noneditable_editable_class', 'mceEditable');
  18. };
  19. var getNonEditableRegExps = function (editor) {
  20. var nonEditableRegExps = editor.getParam('noneditable_regexp', []);
  21. if (nonEditableRegExps && nonEditableRegExps.constructor === RegExp) {
  22. return [nonEditableRegExps];
  23. } else {
  24. return nonEditableRegExps;
  25. }
  26. };
  27. var Settings = {
  28. getNonEditableClass: getNonEditableClass,
  29. getEditableClass: getEditableClass,
  30. getNonEditableRegExps: getNonEditableRegExps
  31. };
  32. var hasClass = function (checkClassName) {
  33. return function (node) {
  34. return (' ' + node.attr('class') + ' ').indexOf(checkClassName) !== -1;
  35. };
  36. };
  37. var replaceMatchWithSpan = function (editor, content, cls) {
  38. return function (match) {
  39. var args = arguments, index = args[args.length - 2];
  40. var prevChar = index > 0 ? content.charAt(index - 1) : '';
  41. if (prevChar === '"') {
  42. return match;
  43. }
  44. if (prevChar === '>') {
  45. var findStartTagIndex = content.lastIndexOf('<', index);
  46. if (findStartTagIndex !== -1) {
  47. var tagHtml = content.substring(findStartTagIndex, index);
  48. if (tagHtml.indexOf('contenteditable="false"') !== -1) {
  49. return match;
  50. }
  51. }
  52. }
  53. return '<span class="' + cls + '" data-mce-content="' + editor.dom.encode(args[0]) + '">' + editor.dom.encode(typeof args[1] === 'string' ? args[1] : args[0]) + '</span>';
  54. };
  55. };
  56. var convertRegExpsToNonEditable = function (editor, nonEditableRegExps, e) {
  57. var i = nonEditableRegExps.length, content = e.content;
  58. if (e.format === 'raw') {
  59. return;
  60. }
  61. while (i--) {
  62. content = content.replace(nonEditableRegExps[i], replaceMatchWithSpan(editor, content, Settings.getNonEditableClass(editor)));
  63. }
  64. e.content = content;
  65. };
  66. var setup = function (editor) {
  67. var editClass, nonEditClass;
  68. var contentEditableAttrName = 'contenteditable';
  69. editClass = ' ' + global$1.trim(Settings.getEditableClass(editor)) + ' ';
  70. nonEditClass = ' ' + global$1.trim(Settings.getNonEditableClass(editor)) + ' ';
  71. var hasEditClass = hasClass(editClass);
  72. var hasNonEditClass = hasClass(nonEditClass);
  73. var nonEditableRegExps = Settings.getNonEditableRegExps(editor);
  74. editor.on('PreInit', function () {
  75. if (nonEditableRegExps.length > 0) {
  76. editor.on('BeforeSetContent', function (e) {
  77. convertRegExpsToNonEditable(editor, nonEditableRegExps, e);
  78. });
  79. }
  80. editor.parser.addAttributeFilter('class', function (nodes) {
  81. var i = nodes.length, node;
  82. while (i--) {
  83. node = nodes[i];
  84. if (hasEditClass(node)) {
  85. node.attr(contentEditableAttrName, 'true');
  86. } else if (hasNonEditClass(node)) {
  87. node.attr(contentEditableAttrName, 'false');
  88. }
  89. }
  90. });
  91. editor.serializer.addAttributeFilter(contentEditableAttrName, function (nodes) {
  92. var i = nodes.length, node;
  93. while (i--) {
  94. node = nodes[i];
  95. if (!hasEditClass(node) && !hasNonEditClass(node)) {
  96. continue;
  97. }
  98. if (nonEditableRegExps.length > 0 && node.attr('data-mce-content')) {
  99. node.name = '#text';
  100. node.type = 3;
  101. node.raw = true;
  102. node.value = node.attr('data-mce-content');
  103. } else {
  104. node.attr(contentEditableAttrName, null);
  105. }
  106. }
  107. });
  108. });
  109. };
  110. var FilterContent = { setup: setup };
  111. function Plugin () {
  112. global.add('noneditable', function (editor) {
  113. FilterContent.setup(editor);
  114. });
  115. }
  116. Plugin();
  117. }());