tinymce_plugins_quickbars.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import {
  2. __commonJS
  3. } from "./chunk-2LSFTFF7.js";
  4. // node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/quickbars/plugin.js
  5. var require_plugin = __commonJS({
  6. "node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/quickbars/plugin.js"() {
  7. (function() {
  8. "use strict";
  9. var global$3 = tinymce.util.Tools.resolve("tinymce.PluginManager");
  10. var unique = 0;
  11. var generate = function(prefix) {
  12. var date = /* @__PURE__ */ new Date();
  13. var time = date.getTime();
  14. var random = Math.floor(Math.random() * 1e9);
  15. unique++;
  16. return prefix + "_" + random + unique + String(time);
  17. };
  18. var createTableHtml = function(cols, rows) {
  19. var html = '<table data-mce-id="mce" style="width: 100%">';
  20. html += "<tbody>";
  21. for (var y = 0; y < rows; y++) {
  22. html += "<tr>";
  23. for (var x = 0; x < cols; x++) {
  24. html += "<td><br></td>";
  25. }
  26. html += "</tr>";
  27. }
  28. html += "</tbody>";
  29. html += "</table>";
  30. return html;
  31. };
  32. var getInsertedElement = function(editor) {
  33. var elms = editor.dom.select("*[data-mce-id]");
  34. return elms[0];
  35. };
  36. var insertTableHtml = function(editor, cols, rows) {
  37. editor.undoManager.transact(function() {
  38. editor.insertContent(createTableHtml(cols, rows));
  39. var tableElm = getInsertedElement(editor);
  40. tableElm.removeAttribute("data-mce-id");
  41. var cellElm = editor.dom.select("td,th", tableElm);
  42. editor.selection.setCursorLocation(cellElm[0], 0);
  43. });
  44. };
  45. var insertTable = function(editor, cols, rows) {
  46. editor.plugins.table ? editor.plugins.table.insertTable(cols, rows) : insertTableHtml(editor, cols, rows);
  47. };
  48. var insertBlob = function(editor, base64, blob) {
  49. var blobCache = editor.editorUpload.blobCache;
  50. var blobInfo = blobCache.create(generate("mceu"), blob, base64);
  51. blobCache.add(blobInfo);
  52. editor.insertContent(editor.dom.createHTML("img", { src: blobInfo.blobUri() }));
  53. };
  54. var global$2 = tinymce.util.Tools.resolve("tinymce.util.Promise");
  55. var blobToBase64 = function(blob) {
  56. return new global$2(function(resolve) {
  57. var reader = new FileReader();
  58. reader.onloadend = function() {
  59. resolve(reader.result.split(",")[1]);
  60. };
  61. reader.readAsDataURL(blob);
  62. });
  63. };
  64. var global$1 = tinymce.util.Tools.resolve("tinymce.Env");
  65. var global = tinymce.util.Tools.resolve("tinymce.util.Delay");
  66. var pickFile = function(editor) {
  67. return new global$2(function(resolve) {
  68. var fileInput = document.createElement("input");
  69. fileInput.type = "file";
  70. fileInput.accept = "image/*";
  71. fileInput.style.position = "fixed";
  72. fileInput.style.left = "0";
  73. fileInput.style.top = "0";
  74. fileInput.style.opacity = "0.001";
  75. document.body.appendChild(fileInput);
  76. var changeHandler = function(e) {
  77. resolve(Array.prototype.slice.call(e.target.files));
  78. };
  79. fileInput.addEventListener("change", changeHandler);
  80. var cancelHandler = function(e) {
  81. var cleanup = function() {
  82. resolve([]);
  83. fileInput.parentNode.removeChild(fileInput);
  84. };
  85. if (global$1.os.isAndroid() && e.type !== "remove") {
  86. global.setEditorTimeout(editor, cleanup, 0);
  87. } else {
  88. cleanup();
  89. }
  90. editor.off("focusin remove", cancelHandler);
  91. };
  92. editor.on("focusin remove", cancelHandler);
  93. fileInput.click();
  94. });
  95. };
  96. var setupButtons = function(editor) {
  97. editor.ui.registry.addButton("quickimage", {
  98. icon: "image",
  99. tooltip: "Insert image",
  100. onAction: function() {
  101. pickFile(editor).then(function(files) {
  102. if (files.length > 0) {
  103. var blob_1 = files[0];
  104. blobToBase64(blob_1).then(function(base64) {
  105. insertBlob(editor, base64, blob_1);
  106. });
  107. }
  108. });
  109. }
  110. });
  111. editor.ui.registry.addButton("quicktable", {
  112. icon: "table",
  113. tooltip: "Insert table",
  114. onAction: function() {
  115. insertTable(editor, 2, 2);
  116. }
  117. });
  118. };
  119. var typeOf = function(x) {
  120. var t = typeof x;
  121. if (x === null) {
  122. return "null";
  123. } else if (t === "object" && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === "Array")) {
  124. return "array";
  125. } else if (t === "object" && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === "String")) {
  126. return "string";
  127. } else {
  128. return t;
  129. }
  130. };
  131. var isType = function(type) {
  132. return function(value) {
  133. return typeOf(value) === type;
  134. };
  135. };
  136. var isSimpleType = function(type) {
  137. return function(value) {
  138. return typeof value === type;
  139. };
  140. };
  141. var eq = function(t) {
  142. return function(a) {
  143. return t === a;
  144. };
  145. };
  146. var isString = isType("string");
  147. var isObject = isType("object");
  148. var isArray = isType("array");
  149. var isBoolean = isSimpleType("boolean");
  150. var isUndefined = eq(void 0);
  151. var isFunction = isSimpleType("function");
  152. var noop = function() {
  153. };
  154. var constant = function(value) {
  155. return function() {
  156. return value;
  157. };
  158. };
  159. var identity = function(x) {
  160. return x;
  161. };
  162. var never = constant(false);
  163. var always = constant(true);
  164. var none = function() {
  165. return NONE;
  166. };
  167. var NONE = function() {
  168. var call = function(thunk) {
  169. return thunk();
  170. };
  171. var id = identity;
  172. var me = {
  173. fold: function(n, _s) {
  174. return n();
  175. },
  176. isSome: never,
  177. isNone: always,
  178. getOr: id,
  179. getOrThunk: call,
  180. getOrDie: function(msg) {
  181. throw new Error(msg || "error: getOrDie called on none.");
  182. },
  183. getOrNull: constant(null),
  184. getOrUndefined: constant(void 0),
  185. or: id,
  186. orThunk: call,
  187. map: none,
  188. each: noop,
  189. bind: none,
  190. exists: never,
  191. forall: always,
  192. filter: function() {
  193. return none();
  194. },
  195. toArray: function() {
  196. return [];
  197. },
  198. toString: constant("none()")
  199. };
  200. return me;
  201. }();
  202. var some = function(a) {
  203. var constant_a = constant(a);
  204. var self = function() {
  205. return me;
  206. };
  207. var bind = function(f) {
  208. return f(a);
  209. };
  210. var me = {
  211. fold: function(n, s) {
  212. return s(a);
  213. },
  214. isSome: always,
  215. isNone: never,
  216. getOr: constant_a,
  217. getOrThunk: constant_a,
  218. getOrDie: constant_a,
  219. getOrNull: constant_a,
  220. getOrUndefined: constant_a,
  221. or: self,
  222. orThunk: self,
  223. map: function(f) {
  224. return some(f(a));
  225. },
  226. each: function(f) {
  227. f(a);
  228. },
  229. bind,
  230. exists: bind,
  231. forall: bind,
  232. filter: function(f) {
  233. return f(a) ? me : NONE;
  234. },
  235. toArray: function() {
  236. return [a];
  237. },
  238. toString: function() {
  239. return "some(" + a + ")";
  240. }
  241. };
  242. return me;
  243. };
  244. var from = function(value) {
  245. return value === null || value === void 0 ? NONE : some(value);
  246. };
  247. var Optional = {
  248. some,
  249. none,
  250. from
  251. };
  252. function ClosestOrAncestor(is2, ancestor2, scope, a, isRoot) {
  253. if (is2(scope, a)) {
  254. return Optional.some(scope);
  255. } else if (isFunction(isRoot) && isRoot(scope)) {
  256. return Optional.none();
  257. } else {
  258. return ancestor2(scope, a, isRoot);
  259. }
  260. }
  261. var ELEMENT = 1;
  262. var fromHtml = function(html, scope) {
  263. var doc = scope || document;
  264. var div = doc.createElement("div");
  265. div.innerHTML = html;
  266. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  267. console.error("HTML does not have a single root node", html);
  268. throw new Error("HTML must have a single root node");
  269. }
  270. return fromDom(div.childNodes[0]);
  271. };
  272. var fromTag = function(tag, scope) {
  273. var doc = scope || document;
  274. var node = doc.createElement(tag);
  275. return fromDom(node);
  276. };
  277. var fromText = function(text, scope) {
  278. var doc = scope || document;
  279. var node = doc.createTextNode(text);
  280. return fromDom(node);
  281. };
  282. var fromDom = function(node) {
  283. if (node === null || node === void 0) {
  284. throw new Error("Node cannot be null or undefined");
  285. }
  286. return { dom: node };
  287. };
  288. var fromPoint = function(docElm, x, y) {
  289. return Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  290. };
  291. var SugarElement = {
  292. fromHtml,
  293. fromTag,
  294. fromText,
  295. fromDom,
  296. fromPoint
  297. };
  298. var is = function(element, selector) {
  299. var dom = element.dom;
  300. if (dom.nodeType !== ELEMENT) {
  301. return false;
  302. } else {
  303. var elem = dom;
  304. if (elem.matches !== void 0) {
  305. return elem.matches(selector);
  306. } else if (elem.msMatchesSelector !== void 0) {
  307. return elem.msMatchesSelector(selector);
  308. } else if (elem.webkitMatchesSelector !== void 0) {
  309. return elem.webkitMatchesSelector(selector);
  310. } else if (elem.mozMatchesSelector !== void 0) {
  311. return elem.mozMatchesSelector(selector);
  312. } else {
  313. throw new Error("Browser lacks native selectors");
  314. }
  315. }
  316. };
  317. typeof window !== "undefined" ? window : Function("return this;")();
  318. var name = function(element) {
  319. var r = element.dom.nodeName;
  320. return r.toLowerCase();
  321. };
  322. var ancestor$1 = function(scope, predicate, isRoot) {
  323. var element = scope.dom;
  324. var stop = isFunction(isRoot) ? isRoot : never;
  325. while (element.parentNode) {
  326. element = element.parentNode;
  327. var el = SugarElement.fromDom(element);
  328. if (predicate(el)) {
  329. return Optional.some(el);
  330. } else if (stop(el)) {
  331. break;
  332. }
  333. }
  334. return Optional.none();
  335. };
  336. var closest$1 = function(scope, predicate, isRoot) {
  337. var is2 = function(s, test) {
  338. return test(s);
  339. };
  340. return ClosestOrAncestor(is2, ancestor$1, scope, predicate, isRoot);
  341. };
  342. var ancestor = function(scope, selector, isRoot) {
  343. return ancestor$1(scope, function(e) {
  344. return is(e, selector);
  345. }, isRoot);
  346. };
  347. var closest = function(scope, selector, isRoot) {
  348. var is$1 = function(element, selector2) {
  349. return is(element, selector2);
  350. };
  351. return ClosestOrAncestor(is$1, ancestor, scope, selector, isRoot);
  352. };
  353. var validDefaultOrDie = function(value, predicate) {
  354. if (predicate(value)) {
  355. return true;
  356. }
  357. throw new Error("Default value doesn't match requested type.");
  358. };
  359. var items = function(value, defaultValue) {
  360. if (isArray(value) || isObject(value)) {
  361. throw new Error("expected a string but found: " + value);
  362. }
  363. if (isUndefined(value)) {
  364. return defaultValue;
  365. }
  366. if (isBoolean(value)) {
  367. return value === false ? "" : defaultValue;
  368. }
  369. return value;
  370. };
  371. var getToolbarItemsOr_ = function(predicate) {
  372. return function(editor, name2, defaultValue) {
  373. validDefaultOrDie(defaultValue, predicate);
  374. var value = editor.getParam(name2, defaultValue);
  375. return items(value, defaultValue);
  376. };
  377. };
  378. var getToolbarItemsOr = getToolbarItemsOr_(isString);
  379. var getTextSelectionToolbarItems = function(editor) {
  380. return getToolbarItemsOr(editor, "quickbars_selection_toolbar", "bold italic | quicklink h2 h3 blockquote");
  381. };
  382. var getInsertToolbarItems = function(editor) {
  383. return getToolbarItemsOr(editor, "quickbars_insert_toolbar", "quickimage quicktable");
  384. };
  385. var getImageToolbarItems = function(editor) {
  386. return getToolbarItemsOr(editor, "quickbars_image_toolbar", "alignleft aligncenter alignright");
  387. };
  388. var addToEditor$1 = function(editor) {
  389. var insertToolbarItems = getInsertToolbarItems(editor);
  390. if (insertToolbarItems.trim().length > 0) {
  391. editor.ui.registry.addContextToolbar("quickblock", {
  392. predicate: function(node) {
  393. var sugarNode = SugarElement.fromDom(node);
  394. var textBlockElementsMap = editor.schema.getTextBlockElements();
  395. var isRoot = function(elem) {
  396. return elem.dom === editor.getBody();
  397. };
  398. return closest(sugarNode, "table", isRoot).fold(function() {
  399. return closest$1(sugarNode, function(elem) {
  400. return name(elem) in textBlockElementsMap && editor.dom.isEmpty(elem.dom);
  401. }, isRoot).isSome();
  402. }, never);
  403. },
  404. items: insertToolbarItems,
  405. position: "line",
  406. scope: "editor"
  407. });
  408. }
  409. };
  410. var addToEditor = function(editor) {
  411. var isEditable = function(node) {
  412. return editor.dom.getContentEditableParent(node) !== "false";
  413. };
  414. var isImage = function(node) {
  415. return node.nodeName === "IMG" || node.nodeName === "FIGURE" && /image/i.test(node.className);
  416. };
  417. var imageToolbarItems = getImageToolbarItems(editor);
  418. if (imageToolbarItems.trim().length > 0) {
  419. editor.ui.registry.addContextToolbar("imageselection", {
  420. predicate: isImage,
  421. items: imageToolbarItems,
  422. position: "node"
  423. });
  424. }
  425. var textToolbarItems = getTextSelectionToolbarItems(editor);
  426. if (textToolbarItems.trim().length > 0) {
  427. editor.ui.registry.addContextToolbar("textselection", {
  428. predicate: function(node) {
  429. return !isImage(node) && !editor.selection.isCollapsed() && isEditable(node);
  430. },
  431. items: textToolbarItems,
  432. position: "selection",
  433. scope: "editor"
  434. });
  435. }
  436. };
  437. function Plugin() {
  438. global$3.add("quickbars", function(editor) {
  439. setupButtons(editor);
  440. addToEditor$1(editor);
  441. addToEditor(editor);
  442. });
  443. }
  444. Plugin();
  445. })();
  446. }
  447. });
  448. // node_modules/.pnpm/tinymce@5.10.7/node_modules/tinymce/plugins/quickbars/index.js
  449. require_plugin();
  450. //# sourceMappingURL=tinymce_plugins_quickbars.js.map