tinymce_plugins_anchor.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import {
  2. __commonJS
  3. } from "./chunk-2LSFTFF7.js";
  4. // node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/anchor/plugin.js
  5. var require_plugin = __commonJS({
  6. "node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/anchor/plugin.js"() {
  7. (function() {
  8. "use strict";
  9. var global$2 = tinymce.util.Tools.resolve("tinymce.PluginManager");
  10. var global$1 = tinymce.util.Tools.resolve("tinymce.dom.RangeUtils");
  11. var global = tinymce.util.Tools.resolve("tinymce.util.Tools");
  12. var allowHtmlInNamedAnchor = function(editor) {
  13. return editor.getParam("allow_html_in_named_anchor", false, "boolean");
  14. };
  15. var namedAnchorSelector = "a:not([href])";
  16. var isEmptyString = function(str) {
  17. return !str;
  18. };
  19. var getIdFromAnchor = function(elm) {
  20. var id = elm.getAttribute("id") || elm.getAttribute("name");
  21. return id || "";
  22. };
  23. var isAnchor = function(elm) {
  24. return elm && elm.nodeName.toLowerCase() === "a";
  25. };
  26. var isNamedAnchor = function(elm) {
  27. return isAnchor(elm) && !elm.getAttribute("href") && getIdFromAnchor(elm) !== "";
  28. };
  29. var isEmptyNamedAnchor = function(elm) {
  30. return isNamedAnchor(elm) && !elm.firstChild;
  31. };
  32. var removeEmptyNamedAnchorsInSelection = function(editor) {
  33. var dom = editor.dom;
  34. global$1(dom).walk(editor.selection.getRng(), function(nodes) {
  35. global.each(nodes, function(node) {
  36. if (isEmptyNamedAnchor(node)) {
  37. dom.remove(node, false);
  38. }
  39. });
  40. });
  41. };
  42. var isValidId = function(id) {
  43. return /^[A-Za-z][A-Za-z0-9\-:._]*$/.test(id);
  44. };
  45. var getNamedAnchor = function(editor) {
  46. return editor.dom.getParent(editor.selection.getStart(), namedAnchorSelector);
  47. };
  48. var getId = function(editor) {
  49. var anchor = getNamedAnchor(editor);
  50. if (anchor) {
  51. return getIdFromAnchor(anchor);
  52. } else {
  53. return "";
  54. }
  55. };
  56. var createAnchor = function(editor, id) {
  57. editor.undoManager.transact(function() {
  58. if (!allowHtmlInNamedAnchor(editor)) {
  59. editor.selection.collapse(true);
  60. }
  61. if (editor.selection.isCollapsed()) {
  62. editor.insertContent(editor.dom.createHTML("a", { id }));
  63. } else {
  64. removeEmptyNamedAnchorsInSelection(editor);
  65. editor.formatter.remove("namedAnchor", null, null, true);
  66. editor.formatter.apply("namedAnchor", { value: id });
  67. editor.addVisual();
  68. }
  69. });
  70. };
  71. var updateAnchor = function(editor, id, anchorElement) {
  72. anchorElement.removeAttribute("name");
  73. anchorElement.id = id;
  74. editor.addVisual();
  75. editor.undoManager.add();
  76. };
  77. var insert = function(editor, id) {
  78. var anchor = getNamedAnchor(editor);
  79. if (anchor) {
  80. updateAnchor(editor, id, anchor);
  81. } else {
  82. createAnchor(editor, id);
  83. }
  84. editor.focus();
  85. };
  86. var insertAnchor = function(editor, newId) {
  87. if (!isValidId(newId)) {
  88. editor.windowManager.alert("Id should start with a letter, followed only by letters, numbers, dashes, dots, colons or underscores.");
  89. return false;
  90. } else {
  91. insert(editor, newId);
  92. return true;
  93. }
  94. };
  95. var open = function(editor) {
  96. var currentId = getId(editor);
  97. editor.windowManager.open({
  98. title: "Anchor",
  99. size: "normal",
  100. body: {
  101. type: "panel",
  102. items: [{
  103. name: "id",
  104. type: "input",
  105. label: "ID",
  106. placeholder: "example"
  107. }]
  108. },
  109. buttons: [
  110. {
  111. type: "cancel",
  112. name: "cancel",
  113. text: "Cancel"
  114. },
  115. {
  116. type: "submit",
  117. name: "save",
  118. text: "Save",
  119. primary: true
  120. }
  121. ],
  122. initialData: { id: currentId },
  123. onSubmit: function(api) {
  124. if (insertAnchor(editor, api.getData().id)) {
  125. api.close();
  126. }
  127. }
  128. });
  129. };
  130. var register$1 = function(editor) {
  131. editor.addCommand("mceAnchor", function() {
  132. open(editor);
  133. });
  134. };
  135. var isNamedAnchorNode = function(node) {
  136. return node && isEmptyString(node.attr("href")) && !isEmptyString(node.attr("id") || node.attr("name"));
  137. };
  138. var isEmptyNamedAnchorNode = function(node) {
  139. return isNamedAnchorNode(node) && !node.firstChild;
  140. };
  141. var setContentEditable = function(state) {
  142. return function(nodes) {
  143. for (var i = 0; i < nodes.length; i++) {
  144. var node = nodes[i];
  145. if (isEmptyNamedAnchorNode(node)) {
  146. node.attr("contenteditable", state);
  147. }
  148. }
  149. };
  150. };
  151. var setup = function(editor) {
  152. editor.on("PreInit", function() {
  153. editor.parser.addNodeFilter("a", setContentEditable("false"));
  154. editor.serializer.addNodeFilter("a", setContentEditable(null));
  155. });
  156. };
  157. var registerFormats = function(editor) {
  158. editor.formatter.register("namedAnchor", {
  159. inline: "a",
  160. selector: namedAnchorSelector,
  161. remove: "all",
  162. split: true,
  163. deep: true,
  164. attributes: { id: "%value" },
  165. onmatch: function(node, _fmt, _itemName) {
  166. return isNamedAnchor(node);
  167. }
  168. });
  169. };
  170. var register = function(editor) {
  171. editor.ui.registry.addToggleButton("anchor", {
  172. icon: "bookmark",
  173. tooltip: "Anchor",
  174. onAction: function() {
  175. return editor.execCommand("mceAnchor");
  176. },
  177. onSetup: function(buttonApi) {
  178. return editor.selection.selectorChangedWithUnbind("a:not([href])", buttonApi.setActive).unbind;
  179. }
  180. });
  181. editor.ui.registry.addMenuItem("anchor", {
  182. icon: "bookmark",
  183. text: "Anchor...",
  184. onAction: function() {
  185. return editor.execCommand("mceAnchor");
  186. }
  187. });
  188. };
  189. function Plugin() {
  190. global$2.add("anchor", function(editor) {
  191. setup(editor);
  192. register$1(editor);
  193. register(editor);
  194. editor.on("PreInit", function() {
  195. registerFormats(editor);
  196. });
  197. });
  198. }
  199. Plugin();
  200. })();
  201. }
  202. });
  203. // node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/anchor/index.js
  204. require_plugin();
  205. //# sourceMappingURL=tinymce_plugins_anchor.js.map