plugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * TinyMCE version 6.0.3 (2022-05-25)
  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, rules;
  156. href = removeCacheSuffix(href);
  157. if (!href || !fileFilter(href, imported) || isSkinContentCss(editor, href)) {
  158. return;
  159. }
  160. global.each(styleSheet.imports, styleSheet => {
  161. append(styleSheet, true);
  162. });
  163. try {
  164. rules = styleSheet.cssRules || styleSheet.rules;
  165. } catch (e) {
  166. }
  167. global.each(rules, cssRule => {
  168. if (isCssImportRule(cssRule)) {
  169. append(cssRule.styleSheet, true);
  170. } else if (isCssPageRule(cssRule)) {
  171. global.each(cssRule.selectorText.split(','), selector => {
  172. selectors.push(global.trim(selector));
  173. });
  174. }
  175. });
  176. };
  177. global.each(editor.contentCSS, url => {
  178. contentCSSUrls[url] = true;
  179. });
  180. if (!fileFilter) {
  181. fileFilter = (href, imported) => {
  182. return imported || contentCSSUrls[href];
  183. };
  184. }
  185. try {
  186. global.each(doc.styleSheets, styleSheet => {
  187. append(styleSheet);
  188. });
  189. } catch (e) {
  190. }
  191. return selectors;
  192. };
  193. const defaultConvertSelectorToFormat = (editor, selectorText) => {
  194. let format;
  195. const selector = /^(?:([a-z0-9\-_]+))?(\.[a-z0-9_\-\.]+)$/i.exec(selectorText);
  196. if (!selector) {
  197. return;
  198. }
  199. const elementName = selector[1];
  200. const classes = selector[2].substr(1).split('.').join(' ');
  201. const inlineSelectorElements = global.makeMap('a,img');
  202. if (selector[1]) {
  203. format = { title: selectorText };
  204. if (editor.schema.getTextBlockElements()[elementName]) {
  205. format.block = elementName;
  206. } else if (editor.schema.getBlockElements()[elementName] || inlineSelectorElements[elementName.toLowerCase()]) {
  207. format.selector = elementName;
  208. } else {
  209. format.inline = elementName;
  210. }
  211. } else if (selector[2]) {
  212. format = {
  213. inline: 'span',
  214. title: selectorText.substr(1),
  215. classes
  216. };
  217. }
  218. if (shouldMergeClasses(editor)) {
  219. format.classes = classes;
  220. } else {
  221. format.attributes = { class: classes };
  222. }
  223. return format;
  224. };
  225. const getGroupsBySelector = (groups, selector) => {
  226. return global.grep(groups, group => {
  227. return !group.filter || group.filter(selector);
  228. });
  229. };
  230. const compileUserDefinedGroups = groups => {
  231. return global.map(groups, group => {
  232. return global.extend({}, group, {
  233. original: group,
  234. selectors: {},
  235. filter: compileFilter(group.filter)
  236. });
  237. });
  238. };
  239. const isExclusiveMode = (editor, group) => {
  240. return group === null || shouldImportExclusive(editor);
  241. };
  242. const isUniqueSelector = (editor, selector, group, globallyUniqueSelectors) => {
  243. return !(isExclusiveMode(editor, group) ? selector in globallyUniqueSelectors : selector in group.selectors);
  244. };
  245. const markUniqueSelector = (editor, selector, group, globallyUniqueSelectors) => {
  246. if (isExclusiveMode(editor, group)) {
  247. globallyUniqueSelectors[selector] = true;
  248. } else {
  249. group.selectors[selector] = true;
  250. }
  251. };
  252. const convertSelectorToFormat = (editor, plugin, selector, group) => {
  253. let selectorConverter;
  254. if (group && group.selector_converter) {
  255. selectorConverter = group.selector_converter;
  256. } else if (getSelectorConverter(editor)) {
  257. selectorConverter = getSelectorConverter(editor);
  258. } else {
  259. selectorConverter = () => {
  260. return defaultConvertSelectorToFormat(editor, selector);
  261. };
  262. }
  263. return selectorConverter.call(plugin, selector, group);
  264. };
  265. const setup = editor => {
  266. editor.on('init', () => {
  267. const model = generate();
  268. const globallyUniqueSelectors = {};
  269. const selectorFilter = compileFilter(getSelectorFilter(editor));
  270. const groups = compileUserDefinedGroups(getCssGroups(editor));
  271. const processSelector = (selector, group) => {
  272. if (isUniqueSelector(editor, selector, group, globallyUniqueSelectors)) {
  273. markUniqueSelector(editor, selector, group, globallyUniqueSelectors);
  274. const format = convertSelectorToFormat(editor, editor.plugins.importcss, selector, group);
  275. if (format) {
  276. const formatName = format.name || global$3.DOM.uniqueId();
  277. editor.formatter.register(formatName, format);
  278. return {
  279. title: format.title,
  280. format: formatName
  281. };
  282. }
  283. }
  284. return null;
  285. };
  286. global.each(getSelectors(editor, editor.getDoc(), compileFilter(getFileFilter(editor))), selector => {
  287. if (!internalEditorStyle.test(selector)) {
  288. if (!selectorFilter || selectorFilter(selector)) {
  289. const selectorGroups = getGroupsBySelector(groups, selector);
  290. if (selectorGroups.length > 0) {
  291. global.each(selectorGroups, group => {
  292. const menuItem = processSelector(selector, group);
  293. if (menuItem) {
  294. model.addItemToGroup(group.title, menuItem);
  295. }
  296. });
  297. } else {
  298. const menuItem = processSelector(selector, null);
  299. if (menuItem) {
  300. model.addItem(menuItem);
  301. }
  302. }
  303. }
  304. }
  305. });
  306. const items = model.toFormats();
  307. editor.dispatch('addStyleModifications', {
  308. items,
  309. replace: !shouldAppend(editor)
  310. });
  311. });
  312. };
  313. const get = editor => {
  314. const convertSelectorToFormat = selectorText => {
  315. return defaultConvertSelectorToFormat(editor, selectorText);
  316. };
  317. return { convertSelectorToFormat };
  318. };
  319. var Plugin = () => {
  320. global$4.add('importcss', editor => {
  321. register(editor);
  322. setup(editor);
  323. return get(editor);
  324. });
  325. };
  326. Plugin();
  327. })();