plugin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. * TinyMCE version 6.4.2 (2023-04-26)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$4 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  7. const hasProto = (v, constructor, predicate) => {
  8. var _a;
  9. if (predicate(v, constructor.prototype)) {
  10. return true;
  11. } else {
  12. return ((_a = v.constructor) === null || _a === void 0 ? void 0 : _a.name) === constructor.name;
  13. }
  14. };
  15. const typeOf = x => {
  16. const t = typeof x;
  17. if (x === null) {
  18. return 'null';
  19. } else if (t === 'object' && Array.isArray(x)) {
  20. return 'array';
  21. } else if (t === 'object' && hasProto(x, String, (o, proto) => proto.isPrototypeOf(o))) {
  22. return 'string';
  23. } else {
  24. return t;
  25. }
  26. };
  27. const isType = type => value => typeOf(value) === type;
  28. const isSimpleType = type => value => typeof value === type;
  29. const isString = isType('string');
  30. const isObject = isType('object');
  31. const isArray = isType('array');
  32. const isFunction = isSimpleType('function');
  33. var global$3 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
  34. var global$2 = tinymce.util.Tools.resolve('tinymce.EditorManager');
  35. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  36. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  37. const option = name => editor => editor.options.get(name);
  38. const register = editor => {
  39. const registerOption = editor.options.register;
  40. const filterProcessor = value => isString(value) || isFunction(value) || isObject(value);
  41. registerOption('importcss_merge_classes', {
  42. processor: 'boolean',
  43. default: true
  44. });
  45. registerOption('importcss_exclusive', {
  46. processor: 'boolean',
  47. default: true
  48. });
  49. registerOption('importcss_selector_converter', { processor: 'function' });
  50. registerOption('importcss_selector_filter', { processor: filterProcessor });
  51. registerOption('importcss_file_filter', { processor: filterProcessor });
  52. registerOption('importcss_groups', { processor: 'object[]' });
  53. registerOption('importcss_append', {
  54. processor: 'boolean',
  55. default: false
  56. });
  57. };
  58. const shouldMergeClasses = option('importcss_merge_classes');
  59. const shouldImportExclusive = option('importcss_exclusive');
  60. const getSelectorConverter = option('importcss_selector_converter');
  61. const getSelectorFilter = option('importcss_selector_filter');
  62. const getCssGroups = option('importcss_groups');
  63. const shouldAppend = option('importcss_append');
  64. const getFileFilter = option('importcss_file_filter');
  65. const getSkin = option('skin');
  66. const getSkinUrl = option('skin_url');
  67. const nativePush = Array.prototype.push;
  68. const map = (xs, f) => {
  69. const len = xs.length;
  70. const r = new Array(len);
  71. for (let i = 0; i < len; i++) {
  72. const x = xs[i];
  73. r[i] = f(x, i);
  74. }
  75. return r;
  76. };
  77. const flatten = xs => {
  78. const r = [];
  79. for (let i = 0, len = xs.length; i < len; ++i) {
  80. if (!isArray(xs[i])) {
  81. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  82. }
  83. nativePush.apply(r, xs[i]);
  84. }
  85. return r;
  86. };
  87. const bind = (xs, f) => flatten(map(xs, f));
  88. const generate = () => {
  89. const ungroupedOrder = [];
  90. const groupOrder = [];
  91. const groups = {};
  92. const addItemToGroup = (groupTitle, itemInfo) => {
  93. if (groups[groupTitle]) {
  94. groups[groupTitle].push(itemInfo);
  95. } else {
  96. groupOrder.push(groupTitle);
  97. groups[groupTitle] = [itemInfo];
  98. }
  99. };
  100. const addItem = itemInfo => {
  101. ungroupedOrder.push(itemInfo);
  102. };
  103. const toFormats = () => {
  104. const groupItems = bind(groupOrder, g => {
  105. const items = groups[g];
  106. return items.length === 0 ? [] : [{
  107. title: g,
  108. items
  109. }];
  110. });
  111. return groupItems.concat(ungroupedOrder);
  112. };
  113. return {
  114. addItemToGroup,
  115. addItem,
  116. toFormats
  117. };
  118. };
  119. const internalEditorStyle = /^\.(?:ephox|tiny-pageembed|mce)(?:[.-]+\w+)+$/;
  120. const removeCacheSuffix = url => {
  121. const cacheSuffix = global$1.cacheSuffix;
  122. if (isString(url)) {
  123. url = url.replace('?' + cacheSuffix, '').replace('&' + cacheSuffix, '');
  124. }
  125. return url;
  126. };
  127. const isSkinContentCss = (editor, href) => {
  128. const skin = getSkin(editor);
  129. if (skin) {
  130. const skinUrlBase = getSkinUrl(editor);
  131. const skinUrl = skinUrlBase ? editor.documentBaseURI.toAbsolute(skinUrlBase) : global$2.baseURL + '/skins/ui/' + skin;
  132. const contentSkinUrlPart = global$2.baseURL + '/skins/content/';
  133. return href === skinUrl + '/content' + (editor.inline ? '.inline' : '') + '.min.css' || href.indexOf(contentSkinUrlPart) !== -1;
  134. }
  135. return false;
  136. };
  137. const compileFilter = filter => {
  138. if (isString(filter)) {
  139. return value => {
  140. return value.indexOf(filter) !== -1;
  141. };
  142. } else if (filter instanceof RegExp) {
  143. return value => {
  144. return filter.test(value);
  145. };
  146. }
  147. return filter;
  148. };
  149. const isCssImportRule = rule => rule.styleSheet;
  150. const isCssPageRule = rule => rule.selectorText;
  151. const getSelectors = (editor, doc, fileFilter) => {
  152. const selectors = [];
  153. const contentCSSUrls = {};
  154. const append = (styleSheet, imported) => {
  155. let href = styleSheet.href;
  156. let rules;
  157. href = removeCacheSuffix(href);
  158. if (!href || fileFilter && !fileFilter(href, imported) || isSkinContentCss(editor, href)) {
  159. return;
  160. }
  161. global.each(styleSheet.imports, styleSheet => {
  162. append(styleSheet, true);
  163. });
  164. try {
  165. rules = styleSheet.cssRules || styleSheet.rules;
  166. } catch (e) {
  167. }
  168. global.each(rules, cssRule => {
  169. if (isCssImportRule(cssRule)) {
  170. append(cssRule.styleSheet, true);
  171. } else if (isCssPageRule(cssRule)) {
  172. global.each(cssRule.selectorText.split(','), selector => {
  173. selectors.push(global.trim(selector));
  174. });
  175. }
  176. });
  177. };
  178. global.each(editor.contentCSS, url => {
  179. contentCSSUrls[url] = true;
  180. });
  181. if (!fileFilter) {
  182. fileFilter = (href, imported) => {
  183. return imported || contentCSSUrls[href];
  184. };
  185. }
  186. try {
  187. global.each(doc.styleSheets, styleSheet => {
  188. append(styleSheet);
  189. });
  190. } catch (e) {
  191. }
  192. return selectors;
  193. };
  194. const defaultConvertSelectorToFormat = (editor, selectorText) => {
  195. let format = {};
  196. const selector = /^(?:([a-z0-9\-_]+))?(\.[a-z0-9_\-\.]+)$/i.exec(selectorText);
  197. if (!selector) {
  198. return;
  199. }
  200. const elementName = selector[1];
  201. const classes = selector[2].substr(1).split('.').join(' ');
  202. const inlineSelectorElements = global.makeMap('a,img');
  203. if (selector[1]) {
  204. format = { title: selectorText };
  205. if (editor.schema.getTextBlockElements()[elementName]) {
  206. format.block = elementName;
  207. } else if (editor.schema.getBlockElements()[elementName] || inlineSelectorElements[elementName.toLowerCase()]) {
  208. format.selector = elementName;
  209. } else {
  210. format.inline = elementName;
  211. }
  212. } else if (selector[2]) {
  213. format = {
  214. inline: 'span',
  215. title: selectorText.substr(1),
  216. classes
  217. };
  218. }
  219. if (shouldMergeClasses(editor)) {
  220. format.classes = classes;
  221. } else {
  222. format.attributes = { class: classes };
  223. }
  224. return format;
  225. };
  226. const getGroupsBySelector = (groups, selector) => {
  227. return global.grep(groups, group => {
  228. return !group.filter || group.filter(selector);
  229. });
  230. };
  231. const compileUserDefinedGroups = groups => {
  232. return global.map(groups, group => {
  233. return global.extend({}, group, {
  234. original: group,
  235. selectors: {},
  236. filter: compileFilter(group.filter)
  237. });
  238. });
  239. };
  240. const isExclusiveMode = (editor, group) => {
  241. return group === null || shouldImportExclusive(editor);
  242. };
  243. const isUniqueSelector = (editor, selector, group, globallyUniqueSelectors) => {
  244. return !(isExclusiveMode(editor, group) ? selector in globallyUniqueSelectors : selector in group.selectors);
  245. };
  246. const markUniqueSelector = (editor, selector, group, globallyUniqueSelectors) => {
  247. if (isExclusiveMode(editor, group)) {
  248. globallyUniqueSelectors[selector] = true;
  249. } else {
  250. group.selectors[selector] = true;
  251. }
  252. };
  253. const convertSelectorToFormat = (editor, plugin, selector, group) => {
  254. let selectorConverter;
  255. const converter = getSelectorConverter(editor);
  256. if (group && group.selector_converter) {
  257. selectorConverter = group.selector_converter;
  258. } else if (converter) {
  259. selectorConverter = converter;
  260. } else {
  261. selectorConverter = () => {
  262. return defaultConvertSelectorToFormat(editor, selector);
  263. };
  264. }
  265. return selectorConverter.call(plugin, selector, group);
  266. };
  267. const setup = editor => {
  268. editor.on('init', () => {
  269. const model = generate();
  270. const globallyUniqueSelectors = {};
  271. const selectorFilter = compileFilter(getSelectorFilter(editor));
  272. const groups = compileUserDefinedGroups(getCssGroups(editor));
  273. const processSelector = (selector, group) => {
  274. if (isUniqueSelector(editor, selector, group, globallyUniqueSelectors)) {
  275. markUniqueSelector(editor, selector, group, globallyUniqueSelectors);
  276. const format = convertSelectorToFormat(editor, editor.plugins.importcss, selector, group);
  277. if (format) {
  278. const formatName = format.name || global$3.DOM.uniqueId();
  279. editor.formatter.register(formatName, format);
  280. return {
  281. title: format.title,
  282. format: formatName
  283. };
  284. }
  285. }
  286. return null;
  287. };
  288. global.each(getSelectors(editor, editor.getDoc(), compileFilter(getFileFilter(editor))), selector => {
  289. if (!internalEditorStyle.test(selector)) {
  290. if (!selectorFilter || selectorFilter(selector)) {
  291. const selectorGroups = getGroupsBySelector(groups, selector);
  292. if (selectorGroups.length > 0) {
  293. global.each(selectorGroups, group => {
  294. const menuItem = processSelector(selector, group);
  295. if (menuItem) {
  296. model.addItemToGroup(group.title, menuItem);
  297. }
  298. });
  299. } else {
  300. const menuItem = processSelector(selector, null);
  301. if (menuItem) {
  302. model.addItem(menuItem);
  303. }
  304. }
  305. }
  306. }
  307. });
  308. const items = model.toFormats();
  309. editor.dispatch('addStyleModifications', {
  310. items,
  311. replace: !shouldAppend(editor)
  312. });
  313. });
  314. };
  315. const get = editor => {
  316. const convertSelectorToFormat = selectorText => {
  317. return defaultConvertSelectorToFormat(editor, selectorText);
  318. };
  319. return { convertSelectorToFormat };
  320. };
  321. var Plugin = () => {
  322. global$4.add('importcss', editor => {
  323. register(editor);
  324. setup(editor);
  325. return get(editor);
  326. });
  327. };
  328. Plugin();
  329. })();