plugin.js 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393
  1. /**
  2. * TinyMCE version 6.1.0 (2022-06-29)
  3. */
  4. (function () {
  5. 'use strict';
  6. var global$3 = 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$1 = type => value => typeOf(value) === type;
  28. const isSimpleType = type => value => typeof value === type;
  29. const eq$1 = t => a => t === a;
  30. const isString = isType$1('string');
  31. const isArray = isType$1('array');
  32. const isBoolean = isSimpleType('boolean');
  33. const isUndefined = eq$1(undefined);
  34. const isNullable = a => a === null || a === undefined;
  35. const isNonNullable = a => !isNullable(a);
  36. const isFunction = isSimpleType('function');
  37. const isNumber = isSimpleType('number');
  38. const noop = () => {
  39. };
  40. const compose1 = (fbc, fab) => a => fbc(fab(a));
  41. const constant = value => {
  42. return () => {
  43. return value;
  44. };
  45. };
  46. const identity = x => {
  47. return x;
  48. };
  49. const tripleEquals = (a, b) => {
  50. return a === b;
  51. };
  52. function curry(fn, ...initialArgs) {
  53. return (...restArgs) => {
  54. const all = initialArgs.concat(restArgs);
  55. return fn.apply(null, all);
  56. };
  57. }
  58. const never = constant(false);
  59. const always = constant(true);
  60. class Optional {
  61. constructor(tag, value) {
  62. this.tag = tag;
  63. this.value = value;
  64. }
  65. static some(value) {
  66. return new Optional(true, value);
  67. }
  68. static none() {
  69. return Optional.singletonNone;
  70. }
  71. fold(onNone, onSome) {
  72. if (this.tag) {
  73. return onSome(this.value);
  74. } else {
  75. return onNone();
  76. }
  77. }
  78. isSome() {
  79. return this.tag;
  80. }
  81. isNone() {
  82. return !this.tag;
  83. }
  84. map(mapper) {
  85. if (this.tag) {
  86. return Optional.some(mapper(this.value));
  87. } else {
  88. return Optional.none();
  89. }
  90. }
  91. bind(binder) {
  92. if (this.tag) {
  93. return binder(this.value);
  94. } else {
  95. return Optional.none();
  96. }
  97. }
  98. exists(predicate) {
  99. return this.tag && predicate(this.value);
  100. }
  101. forall(predicate) {
  102. return !this.tag || predicate(this.value);
  103. }
  104. filter(predicate) {
  105. if (!this.tag || predicate(this.value)) {
  106. return this;
  107. } else {
  108. return Optional.none();
  109. }
  110. }
  111. getOr(replacement) {
  112. return this.tag ? this.value : replacement;
  113. }
  114. or(replacement) {
  115. return this.tag ? this : replacement;
  116. }
  117. getOrThunk(thunk) {
  118. return this.tag ? this.value : thunk();
  119. }
  120. orThunk(thunk) {
  121. return this.tag ? this : thunk();
  122. }
  123. getOrDie(message) {
  124. if (!this.tag) {
  125. throw new Error(message !== null && message !== void 0 ? message : 'Called getOrDie on None');
  126. } else {
  127. return this.value;
  128. }
  129. }
  130. static from(value) {
  131. return isNonNullable(value) ? Optional.some(value) : Optional.none();
  132. }
  133. getOrNull() {
  134. return this.tag ? this.value : null;
  135. }
  136. getOrUndefined() {
  137. return this.value;
  138. }
  139. each(worker) {
  140. if (this.tag) {
  141. worker(this.value);
  142. }
  143. }
  144. toArray() {
  145. return this.tag ? [this.value] : [];
  146. }
  147. toString() {
  148. return this.tag ? `some(${ this.value })` : 'none()';
  149. }
  150. }
  151. Optional.singletonNone = new Optional(false);
  152. const keys = Object.keys;
  153. const hasOwnProperty = Object.hasOwnProperty;
  154. const each$1 = (obj, f) => {
  155. const props = keys(obj);
  156. for (let k = 0, len = props.length; k < len; k++) {
  157. const i = props[k];
  158. const x = obj[i];
  159. f(x, i);
  160. }
  161. };
  162. const objAcc = r => (x, i) => {
  163. r[i] = x;
  164. };
  165. const internalFilter = (obj, pred, onTrue, onFalse) => {
  166. const r = {};
  167. each$1(obj, (x, i) => {
  168. (pred(x, i) ? onTrue : onFalse)(x, i);
  169. });
  170. return r;
  171. };
  172. const filter$1 = (obj, pred) => {
  173. const t = {};
  174. internalFilter(obj, pred, objAcc(t), noop);
  175. return t;
  176. };
  177. const mapToArray = (obj, f) => {
  178. const r = [];
  179. each$1(obj, (value, name) => {
  180. r.push(f(value, name));
  181. });
  182. return r;
  183. };
  184. const values = obj => {
  185. return mapToArray(obj, identity);
  186. };
  187. const size = obj => {
  188. return keys(obj).length;
  189. };
  190. const get$4 = (obj, key) => {
  191. return has(obj, key) ? Optional.from(obj[key]) : Optional.none();
  192. };
  193. const has = (obj, key) => hasOwnProperty.call(obj, key);
  194. const hasNonNullableKey = (obj, key) => has(obj, key) && obj[key] !== undefined && obj[key] !== null;
  195. const nativeIndexOf = Array.prototype.indexOf;
  196. const nativePush = Array.prototype.push;
  197. const rawIndexOf = (ts, t) => nativeIndexOf.call(ts, t);
  198. const contains = (xs, x) => rawIndexOf(xs, x) > -1;
  199. const exists = (xs, pred) => {
  200. for (let i = 0, len = xs.length; i < len; i++) {
  201. const x = xs[i];
  202. if (pred(x, i)) {
  203. return true;
  204. }
  205. }
  206. return false;
  207. };
  208. const range = (num, f) => {
  209. const r = [];
  210. for (let i = 0; i < num; i++) {
  211. r.push(f(i));
  212. }
  213. return r;
  214. };
  215. const map = (xs, f) => {
  216. const len = xs.length;
  217. const r = new Array(len);
  218. for (let i = 0; i < len; i++) {
  219. const x = xs[i];
  220. r[i] = f(x, i);
  221. }
  222. return r;
  223. };
  224. const each = (xs, f) => {
  225. for (let i = 0, len = xs.length; i < len; i++) {
  226. const x = xs[i];
  227. f(x, i);
  228. }
  229. };
  230. const eachr = (xs, f) => {
  231. for (let i = xs.length - 1; i >= 0; i--) {
  232. const x = xs[i];
  233. f(x, i);
  234. }
  235. };
  236. const partition = (xs, pred) => {
  237. const pass = [];
  238. const fail = [];
  239. for (let i = 0, len = xs.length; i < len; i++) {
  240. const x = xs[i];
  241. const arr = pred(x, i) ? pass : fail;
  242. arr.push(x);
  243. }
  244. return {
  245. pass,
  246. fail
  247. };
  248. };
  249. const filter = (xs, pred) => {
  250. const r = [];
  251. for (let i = 0, len = xs.length; i < len; i++) {
  252. const x = xs[i];
  253. if (pred(x, i)) {
  254. r.push(x);
  255. }
  256. }
  257. return r;
  258. };
  259. const foldr = (xs, f, acc) => {
  260. eachr(xs, (x, i) => {
  261. acc = f(acc, x, i);
  262. });
  263. return acc;
  264. };
  265. const foldl = (xs, f, acc) => {
  266. each(xs, (x, i) => {
  267. acc = f(acc, x, i);
  268. });
  269. return acc;
  270. };
  271. const findUntil = (xs, pred, until) => {
  272. for (let i = 0, len = xs.length; i < len; i++) {
  273. const x = xs[i];
  274. if (pred(x, i)) {
  275. return Optional.some(x);
  276. } else if (until(x, i)) {
  277. break;
  278. }
  279. }
  280. return Optional.none();
  281. };
  282. const find = (xs, pred) => {
  283. return findUntil(xs, pred, never);
  284. };
  285. const flatten$1 = xs => {
  286. const r = [];
  287. for (let i = 0, len = xs.length; i < len; ++i) {
  288. if (!isArray(xs[i])) {
  289. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  290. }
  291. nativePush.apply(r, xs[i]);
  292. }
  293. return r;
  294. };
  295. const bind = (xs, f) => flatten$1(map(xs, f));
  296. const forall = (xs, pred) => {
  297. for (let i = 0, len = xs.length; i < len; ++i) {
  298. const x = xs[i];
  299. if (pred(x, i) !== true) {
  300. return false;
  301. }
  302. }
  303. return true;
  304. };
  305. const mapToObject = (xs, f) => {
  306. const r = {};
  307. for (let i = 0, len = xs.length; i < len; i++) {
  308. const x = xs[i];
  309. r[String(x)] = f(x, i);
  310. }
  311. return r;
  312. };
  313. const get$3 = (xs, i) => i >= 0 && i < xs.length ? Optional.some(xs[i]) : Optional.none();
  314. const head = xs => get$3(xs, 0);
  315. const last = xs => get$3(xs, xs.length - 1);
  316. const findMap = (arr, f) => {
  317. for (let i = 0; i < arr.length; i++) {
  318. const r = f(arr[i], i);
  319. if (r.isSome()) {
  320. return r;
  321. }
  322. }
  323. return Optional.none();
  324. };
  325. const fromHtml = (html, scope) => {
  326. const doc = scope || document;
  327. const div = doc.createElement('div');
  328. div.innerHTML = html;
  329. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  330. const message = 'HTML does not have a single root node';
  331. console.error(message, html);
  332. throw new Error(message);
  333. }
  334. return fromDom$1(div.childNodes[0]);
  335. };
  336. const fromTag = (tag, scope) => {
  337. const doc = scope || document;
  338. const node = doc.createElement(tag);
  339. return fromDom$1(node);
  340. };
  341. const fromText = (text, scope) => {
  342. const doc = scope || document;
  343. const node = doc.createTextNode(text);
  344. return fromDom$1(node);
  345. };
  346. const fromDom$1 = node => {
  347. if (node === null || node === undefined) {
  348. throw new Error('Node cannot be null or undefined');
  349. }
  350. return { dom: node };
  351. };
  352. const fromPoint = (docElm, x, y) => Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom$1);
  353. const SugarElement = {
  354. fromHtml,
  355. fromTag,
  356. fromText,
  357. fromDom: fromDom$1,
  358. fromPoint
  359. };
  360. typeof window !== 'undefined' ? window : Function('return this;')();
  361. const COMMENT = 8;
  362. const DOCUMENT = 9;
  363. const DOCUMENT_FRAGMENT = 11;
  364. const ELEMENT = 1;
  365. const TEXT = 3;
  366. const name = element => {
  367. const r = element.dom.nodeName;
  368. return r.toLowerCase();
  369. };
  370. const type = element => element.dom.nodeType;
  371. const isType = t => element => type(element) === t;
  372. const isComment = element => type(element) === COMMENT || name(element) === '#comment';
  373. const isElement = isType(ELEMENT);
  374. const isText = isType(TEXT);
  375. const isDocument = isType(DOCUMENT);
  376. const isDocumentFragment = isType(DOCUMENT_FRAGMENT);
  377. const isTag = tag => e => isElement(e) && name(e) === tag;
  378. const is$2 = (element, selector) => {
  379. const dom = element.dom;
  380. if (dom.nodeType !== ELEMENT) {
  381. return false;
  382. } else {
  383. const elem = dom;
  384. if (elem.matches !== undefined) {
  385. return elem.matches(selector);
  386. } else if (elem.msMatchesSelector !== undefined) {
  387. return elem.msMatchesSelector(selector);
  388. } else if (elem.webkitMatchesSelector !== undefined) {
  389. return elem.webkitMatchesSelector(selector);
  390. } else if (elem.mozMatchesSelector !== undefined) {
  391. return elem.mozMatchesSelector(selector);
  392. } else {
  393. throw new Error('Browser lacks native selectors');
  394. }
  395. }
  396. };
  397. const bypassSelector = dom => dom.nodeType !== ELEMENT && dom.nodeType !== DOCUMENT && dom.nodeType !== DOCUMENT_FRAGMENT || dom.childElementCount === 0;
  398. const all$1 = (selector, scope) => {
  399. const base = scope === undefined ? document : scope.dom;
  400. return bypassSelector(base) ? [] : map(base.querySelectorAll(selector), SugarElement.fromDom);
  401. };
  402. const one = (selector, scope) => {
  403. const base = scope === undefined ? document : scope.dom;
  404. return bypassSelector(base) ? Optional.none() : Optional.from(base.querySelector(selector)).map(SugarElement.fromDom);
  405. };
  406. const eq = (e1, e2) => e1.dom === e2.dom;
  407. const is$1 = is$2;
  408. const owner = element => SugarElement.fromDom(element.dom.ownerDocument);
  409. const documentOrOwner = dos => isDocument(dos) ? dos : owner(dos);
  410. const parent = element => Optional.from(element.dom.parentNode).map(SugarElement.fromDom);
  411. const parents = (element, isRoot) => {
  412. const stop = isFunction(isRoot) ? isRoot : never;
  413. let dom = element.dom;
  414. const ret = [];
  415. while (dom.parentNode !== null && dom.parentNode !== undefined) {
  416. const rawParent = dom.parentNode;
  417. const p = SugarElement.fromDom(rawParent);
  418. ret.push(p);
  419. if (stop(p) === true) {
  420. break;
  421. } else {
  422. dom = rawParent;
  423. }
  424. }
  425. return ret;
  426. };
  427. const prevSibling = element => Optional.from(element.dom.previousSibling).map(SugarElement.fromDom);
  428. const nextSibling = element => Optional.from(element.dom.nextSibling).map(SugarElement.fromDom);
  429. const children$3 = element => map(element.dom.childNodes, SugarElement.fromDom);
  430. const child$3 = (element, index) => {
  431. const cs = element.dom.childNodes;
  432. return Optional.from(cs[index]).map(SugarElement.fromDom);
  433. };
  434. const firstChild = element => child$3(element, 0);
  435. const isShadowRoot = dos => isDocumentFragment(dos) && isNonNullable(dos.dom.host);
  436. const supported = isFunction(Element.prototype.attachShadow) && isFunction(Node.prototype.getRootNode);
  437. const getRootNode = supported ? e => SugarElement.fromDom(e.dom.getRootNode()) : documentOrOwner;
  438. const getShadowRoot = e => {
  439. const r = getRootNode(e);
  440. return isShadowRoot(r) ? Optional.some(r) : Optional.none();
  441. };
  442. const getShadowHost = e => SugarElement.fromDom(e.dom.host);
  443. const inBody = element => {
  444. const dom = isText(element) ? element.dom.parentNode : element.dom;
  445. if (dom === undefined || dom === null || dom.ownerDocument === null) {
  446. return false;
  447. }
  448. const doc = dom.ownerDocument;
  449. return getShadowRoot(SugarElement.fromDom(dom)).fold(() => doc.body.contains(dom), compose1(inBody, getShadowHost));
  450. };
  451. const children$2 = (scope, predicate) => filter(children$3(scope), predicate);
  452. const descendants$1 = (scope, predicate) => {
  453. let result = [];
  454. each(children$3(scope), x => {
  455. if (predicate(x)) {
  456. result = result.concat([x]);
  457. }
  458. result = result.concat(descendants$1(x, predicate));
  459. });
  460. return result;
  461. };
  462. const children$1 = (scope, selector) => children$2(scope, e => is$2(e, selector));
  463. const descendants = (scope, selector) => all$1(selector, scope);
  464. var ClosestOrAncestor = (is, ancestor, scope, a, isRoot) => {
  465. if (is(scope, a)) {
  466. return Optional.some(scope);
  467. } else if (isFunction(isRoot) && isRoot(scope)) {
  468. return Optional.none();
  469. } else {
  470. return ancestor(scope, a, isRoot);
  471. }
  472. };
  473. const ancestor$1 = (scope, predicate, isRoot) => {
  474. let element = scope.dom;
  475. const stop = isFunction(isRoot) ? isRoot : never;
  476. while (element.parentNode) {
  477. element = element.parentNode;
  478. const el = SugarElement.fromDom(element);
  479. if (predicate(el)) {
  480. return Optional.some(el);
  481. } else if (stop(el)) {
  482. break;
  483. }
  484. }
  485. return Optional.none();
  486. };
  487. const child$2 = (scope, predicate) => {
  488. const pred = node => predicate(SugarElement.fromDom(node));
  489. const result = find(scope.dom.childNodes, pred);
  490. return result.map(SugarElement.fromDom);
  491. };
  492. const ancestor = (scope, selector, isRoot) => ancestor$1(scope, e => is$2(e, selector), isRoot);
  493. const child$1 = (scope, selector) => child$2(scope, e => is$2(e, selector));
  494. const descendant = (scope, selector) => one(selector, scope);
  495. const closest = (scope, selector, isRoot) => {
  496. const is = (element, selector) => is$2(element, selector);
  497. return ClosestOrAncestor(is, ancestor, scope, selector, isRoot);
  498. };
  499. const rawSet = (dom, key, value) => {
  500. if (isString(value) || isBoolean(value) || isNumber(value)) {
  501. dom.setAttribute(key, value + '');
  502. } else {
  503. console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  504. throw new Error('Attribute value was not simple');
  505. }
  506. };
  507. const set$2 = (element, key, value) => {
  508. rawSet(element.dom, key, value);
  509. };
  510. const setAll = (element, attrs) => {
  511. const dom = element.dom;
  512. each$1(attrs, (v, k) => {
  513. rawSet(dom, k, v);
  514. });
  515. };
  516. const get$2 = (element, key) => {
  517. const v = element.dom.getAttribute(key);
  518. return v === null ? undefined : v;
  519. };
  520. const getOpt = (element, key) => Optional.from(get$2(element, key));
  521. const remove$2 = (element, key) => {
  522. element.dom.removeAttribute(key);
  523. };
  524. const clone = element => foldl(element.dom.attributes, (acc, attr) => {
  525. acc[attr.name] = attr.value;
  526. return acc;
  527. }, {});
  528. const is = (lhs, rhs, comparator = tripleEquals) => lhs.exists(left => comparator(left, rhs));
  529. const cat = arr => {
  530. const r = [];
  531. const push = x => {
  532. r.push(x);
  533. };
  534. for (let i = 0; i < arr.length; i++) {
  535. arr[i].each(push);
  536. }
  537. return r;
  538. };
  539. const lift2 = (oa, ob, f) => oa.isSome() && ob.isSome() ? Optional.some(f(oa.getOrDie(), ob.getOrDie())) : Optional.none();
  540. const flatten = oot => oot.bind(identity);
  541. const someIf = (b, a) => b ? Optional.some(a) : Optional.none();
  542. const removeFromStart = (str, numChars) => {
  543. return str.substring(numChars);
  544. };
  545. const checkRange = (str, substr, start) => substr === '' || str.length >= substr.length && str.substr(start, start + substr.length) === substr;
  546. const removeLeading = (str, prefix) => {
  547. return startsWith(str, prefix) ? removeFromStart(str, prefix.length) : str;
  548. };
  549. const startsWith = (str, prefix) => {
  550. return checkRange(str, prefix, 0);
  551. };
  552. const blank = r => s => s.replace(r, '');
  553. const trim = blank(/^\s+|\s+$/g);
  554. const isNotEmpty = s => s.length > 0;
  555. const isEmpty = s => !isNotEmpty(s);
  556. const toFloat = value => {
  557. const num = parseFloat(value);
  558. return isNaN(num) ? Optional.none() : Optional.some(num);
  559. };
  560. const isSupported = dom => dom.style !== undefined && isFunction(dom.style.getPropertyValue);
  561. const internalSet = (dom, property, value) => {
  562. if (!isString(value)) {
  563. console.error('Invalid call to CSS.set. Property ', property, ':: Value ', value, ':: Element ', dom);
  564. throw new Error('CSS value must be a string: ' + value);
  565. }
  566. if (isSupported(dom)) {
  567. dom.style.setProperty(property, value);
  568. }
  569. };
  570. const internalRemove = (dom, property) => {
  571. if (isSupported(dom)) {
  572. dom.style.removeProperty(property);
  573. }
  574. };
  575. const set$1 = (element, property, value) => {
  576. const dom = element.dom;
  577. internalSet(dom, property, value);
  578. };
  579. const get$1 = (element, property) => {
  580. const dom = element.dom;
  581. const styles = window.getComputedStyle(dom);
  582. const r = styles.getPropertyValue(property);
  583. return r === '' && !inBody(element) ? getUnsafeProperty(dom, property) : r;
  584. };
  585. const getUnsafeProperty = (dom, property) => isSupported(dom) ? dom.style.getPropertyValue(property) : '';
  586. const getRaw = (element, property) => {
  587. const dom = element.dom;
  588. const raw = getUnsafeProperty(dom, property);
  589. return Optional.from(raw).filter(r => r.length > 0);
  590. };
  591. const remove$1 = (element, property) => {
  592. const dom = element.dom;
  593. internalRemove(dom, property);
  594. if (is(getOpt(element, 'style').map(trim), '')) {
  595. remove$2(element, 'style');
  596. }
  597. };
  598. const getAttrValue = (cell, name, fallback = 0) => getOpt(cell, name).map(value => parseInt(value, 10)).getOr(fallback);
  599. const firstLayer = (scope, selector) => {
  600. return filterFirstLayer(scope, selector, always);
  601. };
  602. const filterFirstLayer = (scope, selector, predicate) => {
  603. return bind(children$3(scope), x => {
  604. if (is$2(x, selector)) {
  605. return predicate(x) ? [x] : [];
  606. } else {
  607. return filterFirstLayer(x, selector, predicate);
  608. }
  609. });
  610. };
  611. const validSectionList = [
  612. 'tfoot',
  613. 'thead',
  614. 'tbody',
  615. 'colgroup'
  616. ];
  617. const isValidSection = parentName => contains(validSectionList, parentName);
  618. const grid = (rows, columns) => ({
  619. rows,
  620. columns
  621. });
  622. const detail = (element, rowspan, colspan) => ({
  623. element,
  624. rowspan,
  625. colspan
  626. });
  627. const extended = (element, rowspan, colspan, row, column, isLocked) => ({
  628. element,
  629. rowspan,
  630. colspan,
  631. row,
  632. column,
  633. isLocked
  634. });
  635. const rowdetail = (element, cells, section) => ({
  636. element,
  637. cells,
  638. section
  639. });
  640. const bounds = (startRow, startCol, finishRow, finishCol) => ({
  641. startRow,
  642. startCol,
  643. finishRow,
  644. finishCol
  645. });
  646. const columnext = (element, colspan, column) => ({
  647. element,
  648. colspan,
  649. column
  650. });
  651. const colgroup = (element, columns) => ({
  652. element,
  653. columns
  654. });
  655. const lookup = (tags, element, isRoot = never) => {
  656. if (isRoot(element)) {
  657. return Optional.none();
  658. }
  659. if (contains(tags, name(element))) {
  660. return Optional.some(element);
  661. }
  662. const isRootOrUpperTable = elm => is$2(elm, 'table') || isRoot(elm);
  663. return ancestor(element, tags.join(','), isRootOrUpperTable);
  664. };
  665. const cell = (element, isRoot) => lookup([
  666. 'td',
  667. 'th'
  668. ], element, isRoot);
  669. const cells = ancestor => firstLayer(ancestor, 'th,td');
  670. const columns = ancestor => {
  671. if (is$2(ancestor, 'colgroup')) {
  672. return children$1(ancestor, 'col');
  673. } else {
  674. return bind(columnGroups(ancestor), columnGroup => children$1(columnGroup, 'col'));
  675. }
  676. };
  677. const table = (element, isRoot) => closest(element, 'table', isRoot);
  678. const rows = ancestor => firstLayer(ancestor, 'tr');
  679. const columnGroups = ancestor => table(ancestor).fold(constant([]), table => children$1(table, 'colgroup'));
  680. const fromRowsOrColGroups = (elems, getSection) => map(elems, row => {
  681. if (name(row) === 'colgroup') {
  682. const cells = map(columns(row), column => {
  683. const colspan = getAttrValue(column, 'span', 1);
  684. return detail(column, 1, colspan);
  685. });
  686. return rowdetail(row, cells, 'colgroup');
  687. } else {
  688. const cells$1 = map(cells(row), cell => {
  689. const rowspan = getAttrValue(cell, 'rowspan', 1);
  690. const colspan = getAttrValue(cell, 'colspan', 1);
  691. return detail(cell, rowspan, colspan);
  692. });
  693. return rowdetail(row, cells$1, getSection(row));
  694. }
  695. });
  696. const getParentSection = group => parent(group).map(parent => {
  697. const parentName = name(parent);
  698. return isValidSection(parentName) ? parentName : 'tbody';
  699. }).getOr('tbody');
  700. const fromTable$1 = table => {
  701. const rows$1 = rows(table);
  702. const columnGroups$1 = columnGroups(table);
  703. const elems = [
  704. ...columnGroups$1,
  705. ...rows$1
  706. ];
  707. return fromRowsOrColGroups(elems, getParentSection);
  708. };
  709. const LOCKED_COL_ATTR = 'data-snooker-locked-cols';
  710. const getLockedColumnsFromTable = table => getOpt(table, LOCKED_COL_ATTR).bind(lockedColStr => Optional.from(lockedColStr.match(/\d+/g))).map(lockedCols => mapToObject(lockedCols, always));
  711. const key = (row, column) => {
  712. return row + ',' + column;
  713. };
  714. const getAt = (warehouse, row, column) => Optional.from(warehouse.access[key(row, column)]);
  715. const findItem = (warehouse, item, comparator) => {
  716. const filtered = filterItems(warehouse, detail => {
  717. return comparator(item, detail.element);
  718. });
  719. return filtered.length > 0 ? Optional.some(filtered[0]) : Optional.none();
  720. };
  721. const filterItems = (warehouse, predicate) => {
  722. const all = bind(warehouse.all, r => {
  723. return r.cells;
  724. });
  725. return filter(all, predicate);
  726. };
  727. const generateColumns = rowData => {
  728. const columnsGroup = {};
  729. let index = 0;
  730. each(rowData.cells, column => {
  731. const colspan = column.colspan;
  732. range(colspan, columnIndex => {
  733. const colIndex = index + columnIndex;
  734. columnsGroup[colIndex] = columnext(column.element, colspan, colIndex);
  735. });
  736. index += colspan;
  737. });
  738. return columnsGroup;
  739. };
  740. const generate$1 = list => {
  741. const access = {};
  742. const cells = [];
  743. const tableOpt = head(list).map(rowData => rowData.element).bind(table);
  744. const lockedColumns = tableOpt.bind(getLockedColumnsFromTable).getOr({});
  745. let maxRows = 0;
  746. let maxColumns = 0;
  747. let rowCount = 0;
  748. const {
  749. pass: colgroupRows,
  750. fail: rows
  751. } = partition(list, rowData => rowData.section === 'colgroup');
  752. each(rows, rowData => {
  753. const currentRow = [];
  754. each(rowData.cells, rowCell => {
  755. let start = 0;
  756. while (access[key(rowCount, start)] !== undefined) {
  757. start++;
  758. }
  759. const isLocked = hasNonNullableKey(lockedColumns, start.toString());
  760. const current = extended(rowCell.element, rowCell.rowspan, rowCell.colspan, rowCount, start, isLocked);
  761. for (let occupiedColumnPosition = 0; occupiedColumnPosition < rowCell.colspan; occupiedColumnPosition++) {
  762. for (let occupiedRowPosition = 0; occupiedRowPosition < rowCell.rowspan; occupiedRowPosition++) {
  763. const rowPosition = rowCount + occupiedRowPosition;
  764. const columnPosition = start + occupiedColumnPosition;
  765. const newpos = key(rowPosition, columnPosition);
  766. access[newpos] = current;
  767. maxColumns = Math.max(maxColumns, columnPosition + 1);
  768. }
  769. }
  770. currentRow.push(current);
  771. });
  772. maxRows++;
  773. cells.push(rowdetail(rowData.element, currentRow, rowData.section));
  774. rowCount++;
  775. });
  776. const {columns, colgroups} = last(colgroupRows).map(rowData => {
  777. const columns = generateColumns(rowData);
  778. const colgroup$1 = colgroup(rowData.element, values(columns));
  779. return {
  780. colgroups: [colgroup$1],
  781. columns
  782. };
  783. }).getOrThunk(() => ({
  784. colgroups: [],
  785. columns: {}
  786. }));
  787. const grid$1 = grid(maxRows, maxColumns);
  788. return {
  789. grid: grid$1,
  790. access,
  791. all: cells,
  792. columns,
  793. colgroups
  794. };
  795. };
  796. const fromTable = table => {
  797. const list = fromTable$1(table);
  798. return generate$1(list);
  799. };
  800. const justCells = warehouse => bind(warehouse.all, w => w.cells);
  801. const justColumns = warehouse => values(warehouse.columns);
  802. const hasColumns = warehouse => keys(warehouse.columns).length > 0;
  803. const getColumnAt = (warehouse, columnIndex) => Optional.from(warehouse.columns[columnIndex]);
  804. const Warehouse = {
  805. fromTable,
  806. generate: generate$1,
  807. getAt,
  808. findItem,
  809. filterItems,
  810. justCells,
  811. justColumns,
  812. hasColumns,
  813. getColumnAt
  814. };
  815. var global$2 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  816. const getTDTHOverallStyle = (dom, elm, name) => {
  817. const cells = dom.select('td,th', elm);
  818. let firstChildStyle;
  819. const checkChildren = (firstChildStyle, elms) => {
  820. for (let i = 0; i < elms.length; i++) {
  821. const currentStyle = dom.getStyle(elms[i], name);
  822. if (typeof firstChildStyle === 'undefined') {
  823. firstChildStyle = currentStyle;
  824. }
  825. if (firstChildStyle !== currentStyle) {
  826. return '';
  827. }
  828. }
  829. return firstChildStyle;
  830. };
  831. return checkChildren(firstChildStyle, cells);
  832. };
  833. const setAlign = (editor, elm, name) => {
  834. global$2.each('left center right'.split(' '), align => {
  835. if (align !== name) {
  836. editor.formatter.remove('align' + align, {}, elm);
  837. }
  838. });
  839. if (name) {
  840. editor.formatter.apply('align' + name, {}, elm);
  841. }
  842. };
  843. const setVAlign = (editor, elm, name) => {
  844. global$2.each('top middle bottom'.split(' '), align => {
  845. if (align !== name) {
  846. editor.formatter.remove('valign' + align, {}, elm);
  847. }
  848. });
  849. if (name) {
  850. editor.formatter.apply('valign' + name, {}, elm);
  851. }
  852. };
  853. const fireTableModified = (editor, table, data) => {
  854. editor.dispatch('TableModified', {
  855. ...data,
  856. table
  857. });
  858. };
  859. const toNumber = (px, fallback) => toFloat(px).getOr(fallback);
  860. const getProp = (element, name, fallback) => toNumber(get$1(element, name), fallback);
  861. const calcContentBoxSize = (element, size, upper, lower) => {
  862. const paddingUpper = getProp(element, `padding-${ upper }`, 0);
  863. const paddingLower = getProp(element, `padding-${ lower }`, 0);
  864. const borderUpper = getProp(element, `border-${ upper }-width`, 0);
  865. const borderLower = getProp(element, `border-${ lower }-width`, 0);
  866. return size - paddingUpper - paddingLower - borderUpper - borderLower;
  867. };
  868. const getCalculatedWidth = (element, boxSizing) => {
  869. const dom = element.dom;
  870. const width = dom.getBoundingClientRect().width || dom.offsetWidth;
  871. return boxSizing === 'border-box' ? width : calcContentBoxSize(element, width, 'left', 'right');
  872. };
  873. const getInnerWidth = element => getCalculatedWidth(element, 'content-box');
  874. const getInner = getInnerWidth;
  875. var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
  876. const defaultTableToolbar = 'tableprops tabledelete | tableinsertrowbefore tableinsertrowafter tabledeleterow | tableinsertcolbefore tableinsertcolafter tabledeletecol';
  877. const defaultCellBorderWidths = range(5, i => {
  878. const size = `${ i + 1 }px`;
  879. return {
  880. title: size,
  881. value: size
  882. };
  883. });
  884. const defaultCellBorderStyles = map([
  885. 'Solid',
  886. 'Dotted',
  887. 'Dashed',
  888. 'Double',
  889. 'Groove',
  890. 'Ridge',
  891. 'Inset',
  892. 'Outset',
  893. 'None',
  894. 'Hidden'
  895. ], type => {
  896. return {
  897. title: type,
  898. value: type.toLowerCase()
  899. };
  900. });
  901. const defaultWidth = '100%';
  902. const getPixelForcedWidth = editor => {
  903. var _a;
  904. const dom = editor.dom;
  905. const parentBlock = (_a = dom.getParent(editor.selection.getStart(), dom.isBlock)) !== null && _a !== void 0 ? _a : editor.getBody();
  906. return getInner(SugarElement.fromDom(parentBlock)) + 'px';
  907. };
  908. const determineDefaultStyles = (editor, defaultStyles) => {
  909. if (isResponsiveForced(editor) || !shouldStyleWithCss(editor)) {
  910. return defaultStyles;
  911. } else if (isPixelsForced(editor)) {
  912. return {
  913. ...defaultStyles,
  914. width: getPixelForcedWidth(editor)
  915. };
  916. } else {
  917. return {
  918. ...defaultStyles,
  919. width: defaultWidth
  920. };
  921. }
  922. };
  923. const determineDefaultAttributes = (editor, defaultAttributes) => {
  924. if (isResponsiveForced(editor) || shouldStyleWithCss(editor)) {
  925. return defaultAttributes;
  926. } else if (isPixelsForced(editor)) {
  927. return {
  928. ...defaultAttributes,
  929. width: getPixelForcedWidth(editor)
  930. };
  931. } else {
  932. return {
  933. ...defaultAttributes,
  934. width: defaultWidth
  935. };
  936. }
  937. };
  938. const option = name => editor => editor.options.get(name);
  939. const register = editor => {
  940. const registerOption = editor.options.register;
  941. registerOption('table_border_widths', {
  942. processor: 'object[]',
  943. default: defaultCellBorderWidths
  944. });
  945. registerOption('table_border_styles', {
  946. processor: 'object[]',
  947. default: defaultCellBorderStyles
  948. });
  949. registerOption('table_cell_advtab', {
  950. processor: 'boolean',
  951. default: true
  952. });
  953. registerOption('table_row_advtab', {
  954. processor: 'boolean',
  955. default: true
  956. });
  957. registerOption('table_advtab', {
  958. processor: 'boolean',
  959. default: true
  960. });
  961. registerOption('table_appearance_options', {
  962. processor: 'boolean',
  963. default: true
  964. });
  965. registerOption('table_grid', {
  966. processor: 'boolean',
  967. default: !global$1.deviceType.isTouch()
  968. });
  969. registerOption('table_cell_class_list', {
  970. processor: 'object[]',
  971. default: []
  972. });
  973. registerOption('table_row_class_list', {
  974. processor: 'object[]',
  975. default: []
  976. });
  977. registerOption('table_class_list', {
  978. processor: 'object[]',
  979. default: []
  980. });
  981. registerOption('table_toolbar', {
  982. processor: 'string',
  983. default: defaultTableToolbar
  984. });
  985. registerOption('table_background_color_map', {
  986. processor: 'object[]',
  987. default: []
  988. });
  989. registerOption('table_border_color_map', {
  990. processor: 'object[]',
  991. default: []
  992. });
  993. };
  994. const getTableSizingMode = option('table_sizing_mode');
  995. const getTableBorderWidths = option('table_border_widths');
  996. const getTableBorderStyles = option('table_border_styles');
  997. const hasAdvancedCellTab = option('table_cell_advtab');
  998. const hasAdvancedRowTab = option('table_row_advtab');
  999. const hasAdvancedTableTab = option('table_advtab');
  1000. const hasAppearanceOptions = option('table_appearance_options');
  1001. const hasTableGrid = option('table_grid');
  1002. const shouldStyleWithCss = option('table_style_by_css');
  1003. const getCellClassList = option('table_cell_class_list');
  1004. const getRowClassList = option('table_row_class_list');
  1005. const getTableClassList = option('table_class_list');
  1006. const getToolbar = option('table_toolbar');
  1007. const getTableBackgroundColorMap = option('table_background_color_map');
  1008. const getTableBorderColorMap = option('table_border_color_map');
  1009. const isPixelsForced = editor => getTableSizingMode(editor) === 'fixed';
  1010. const isResponsiveForced = editor => getTableSizingMode(editor) === 'responsive';
  1011. const getDefaultStyles = editor => {
  1012. const options = editor.options;
  1013. const defaultStyles = options.get('table_default_styles');
  1014. return options.isSet('table_default_styles') ? defaultStyles : determineDefaultStyles(editor, defaultStyles);
  1015. };
  1016. const getDefaultAttributes = editor => {
  1017. const options = editor.options;
  1018. const defaultAttributes = options.get('table_default_attributes');
  1019. return options.isSet('table_default_attributes') ? defaultAttributes : determineDefaultAttributes(editor, defaultAttributes);
  1020. };
  1021. const getNodeName = elm => elm.nodeName.toLowerCase();
  1022. const getBody = editor => SugarElement.fromDom(editor.getBody());
  1023. const getIsRoot = editor => element => eq(element, getBody(editor));
  1024. const removePxSuffix = size => size ? size.replace(/px$/, '') : '';
  1025. const addPxSuffix = size => /^\d+(\.\d+)?$/.test(size) ? size + 'px' : size;
  1026. const getSelectionStart = editor => SugarElement.fromDom(editor.selection.getStart());
  1027. const getSelectionEnd = editor => SugarElement.fromDom(editor.selection.getEnd());
  1028. const isWithin = (bounds, detail) => {
  1029. return detail.column >= bounds.startCol && detail.column + detail.colspan - 1 <= bounds.finishCol && detail.row >= bounds.startRow && detail.row + detail.rowspan - 1 <= bounds.finishRow;
  1030. };
  1031. const isRectangular = (warehouse, bounds) => {
  1032. let isRect = true;
  1033. const detailIsWithin = curry(isWithin, bounds);
  1034. for (let i = bounds.startRow; i <= bounds.finishRow; i++) {
  1035. for (let j = bounds.startCol; j <= bounds.finishCol; j++) {
  1036. isRect = isRect && Warehouse.getAt(warehouse, i, j).exists(detailIsWithin);
  1037. }
  1038. }
  1039. return isRect ? Optional.some(bounds) : Optional.none();
  1040. };
  1041. const getBounds = (detailA, detailB) => {
  1042. return bounds(Math.min(detailA.row, detailB.row), Math.min(detailA.column, detailB.column), Math.max(detailA.row + detailA.rowspan - 1, detailB.row + detailB.rowspan - 1), Math.max(detailA.column + detailA.colspan - 1, detailB.column + detailB.colspan - 1));
  1043. };
  1044. const getAnyBox = (warehouse, startCell, finishCell) => {
  1045. const startCoords = Warehouse.findItem(warehouse, startCell, eq);
  1046. const finishCoords = Warehouse.findItem(warehouse, finishCell, eq);
  1047. return startCoords.bind(sc => {
  1048. return finishCoords.map(fc => {
  1049. return getBounds(sc, fc);
  1050. });
  1051. });
  1052. };
  1053. const getBox$1 = (warehouse, startCell, finishCell) => {
  1054. return getAnyBox(warehouse, startCell, finishCell).bind(bounds => {
  1055. return isRectangular(warehouse, bounds);
  1056. });
  1057. };
  1058. const getBox = (table, first, last) => {
  1059. const warehouse = getWarehouse(table);
  1060. return getBox$1(warehouse, first, last);
  1061. };
  1062. const getWarehouse = Warehouse.fromTable;
  1063. const before = (marker, element) => {
  1064. const parent$1 = parent(marker);
  1065. parent$1.each(v => {
  1066. v.dom.insertBefore(element.dom, marker.dom);
  1067. });
  1068. };
  1069. const after$1 = (marker, element) => {
  1070. const sibling = nextSibling(marker);
  1071. sibling.fold(() => {
  1072. const parent$1 = parent(marker);
  1073. parent$1.each(v => {
  1074. append$1(v, element);
  1075. });
  1076. }, v => {
  1077. before(v, element);
  1078. });
  1079. };
  1080. const prepend = (parent, element) => {
  1081. const firstChild$1 = firstChild(parent);
  1082. firstChild$1.fold(() => {
  1083. append$1(parent, element);
  1084. }, v => {
  1085. parent.dom.insertBefore(element.dom, v.dom);
  1086. });
  1087. };
  1088. const append$1 = (parent, element) => {
  1089. parent.dom.appendChild(element.dom);
  1090. };
  1091. const wrap = (element, wrapper) => {
  1092. before(element, wrapper);
  1093. append$1(wrapper, element);
  1094. };
  1095. const after = (marker, elements) => {
  1096. each(elements, (x, i) => {
  1097. const e = i === 0 ? marker : elements[i - 1];
  1098. after$1(e, x);
  1099. });
  1100. };
  1101. const append = (parent, elements) => {
  1102. each(elements, x => {
  1103. append$1(parent, x);
  1104. });
  1105. };
  1106. const remove = element => {
  1107. const dom = element.dom;
  1108. if (dom.parentNode !== null) {
  1109. dom.parentNode.removeChild(dom);
  1110. }
  1111. };
  1112. const unwrap = wrapper => {
  1113. const children = children$3(wrapper);
  1114. if (children.length > 0) {
  1115. after(wrapper, children);
  1116. }
  1117. remove(wrapper);
  1118. };
  1119. const NodeValue = (is, name) => {
  1120. const get = element => {
  1121. if (!is(element)) {
  1122. throw new Error('Can only get ' + name + ' value of a ' + name + ' node');
  1123. }
  1124. return getOption(element).getOr('');
  1125. };
  1126. const getOption = element => is(element) ? Optional.from(element.dom.nodeValue) : Optional.none();
  1127. const set = (element, value) => {
  1128. if (!is(element)) {
  1129. throw new Error('Can only set raw ' + name + ' value of a ' + name + ' node');
  1130. }
  1131. element.dom.nodeValue = value;
  1132. };
  1133. return {
  1134. get,
  1135. getOption,
  1136. set
  1137. };
  1138. };
  1139. const api = NodeValue(isText, 'text');
  1140. const get = element => api.get(element);
  1141. const set = (element, value) => api.set(element, value);
  1142. var TagBoundaries = [
  1143. 'body',
  1144. 'p',
  1145. 'div',
  1146. 'article',
  1147. 'aside',
  1148. 'figcaption',
  1149. 'figure',
  1150. 'footer',
  1151. 'header',
  1152. 'nav',
  1153. 'section',
  1154. 'ol',
  1155. 'ul',
  1156. 'li',
  1157. 'table',
  1158. 'thead',
  1159. 'tbody',
  1160. 'tfoot',
  1161. 'caption',
  1162. 'tr',
  1163. 'td',
  1164. 'th',
  1165. 'h1',
  1166. 'h2',
  1167. 'h3',
  1168. 'h4',
  1169. 'h5',
  1170. 'h6',
  1171. 'blockquote',
  1172. 'pre',
  1173. 'address'
  1174. ];
  1175. var DomUniverse = () => {
  1176. const clone$1 = element => {
  1177. return SugarElement.fromDom(element.dom.cloneNode(false));
  1178. };
  1179. const document = element => documentOrOwner(element).dom;
  1180. const isBoundary = element => {
  1181. if (!isElement(element)) {
  1182. return false;
  1183. }
  1184. if (name(element) === 'body') {
  1185. return true;
  1186. }
  1187. return contains(TagBoundaries, name(element));
  1188. };
  1189. const isEmptyTag = element => {
  1190. if (!isElement(element)) {
  1191. return false;
  1192. }
  1193. return contains([
  1194. 'br',
  1195. 'img',
  1196. 'hr',
  1197. 'input'
  1198. ], name(element));
  1199. };
  1200. const isNonEditable = element => isElement(element) && get$2(element, 'contenteditable') === 'false';
  1201. const comparePosition = (element, other) => {
  1202. return element.dom.compareDocumentPosition(other.dom);
  1203. };
  1204. const copyAttributesTo = (source, destination) => {
  1205. const as = clone(source);
  1206. setAll(destination, as);
  1207. };
  1208. const isSpecial = element => {
  1209. const tag = name(element);
  1210. return contains([
  1211. 'script',
  1212. 'noscript',
  1213. 'iframe',
  1214. 'noframes',
  1215. 'noembed',
  1216. 'title',
  1217. 'style',
  1218. 'textarea',
  1219. 'xmp'
  1220. ], tag);
  1221. };
  1222. const getLanguage = element => isElement(element) ? getOpt(element, 'lang') : Optional.none();
  1223. return {
  1224. up: constant({
  1225. selector: ancestor,
  1226. closest: closest,
  1227. predicate: ancestor$1,
  1228. all: parents
  1229. }),
  1230. down: constant({
  1231. selector: descendants,
  1232. predicate: descendants$1
  1233. }),
  1234. styles: constant({
  1235. get: get$1,
  1236. getRaw: getRaw,
  1237. set: set$1,
  1238. remove: remove$1
  1239. }),
  1240. attrs: constant({
  1241. get: get$2,
  1242. set: set$2,
  1243. remove: remove$2,
  1244. copyTo: copyAttributesTo
  1245. }),
  1246. insert: constant({
  1247. before: before,
  1248. after: after$1,
  1249. afterAll: after,
  1250. append: append$1,
  1251. appendAll: append,
  1252. prepend: prepend,
  1253. wrap: wrap
  1254. }),
  1255. remove: constant({
  1256. unwrap: unwrap,
  1257. remove: remove
  1258. }),
  1259. create: constant({
  1260. nu: SugarElement.fromTag,
  1261. clone: clone$1,
  1262. text: SugarElement.fromText
  1263. }),
  1264. query: constant({
  1265. comparePosition,
  1266. prevSibling: prevSibling,
  1267. nextSibling: nextSibling
  1268. }),
  1269. property: constant({
  1270. children: children$3,
  1271. name: name,
  1272. parent: parent,
  1273. document,
  1274. isText: isText,
  1275. isComment: isComment,
  1276. isElement: isElement,
  1277. isSpecial,
  1278. getLanguage,
  1279. getText: get,
  1280. setText: set,
  1281. isBoundary,
  1282. isEmptyTag,
  1283. isNonEditable
  1284. }),
  1285. eq: eq,
  1286. is: is$1
  1287. };
  1288. };
  1289. const all = (universe, look, elements, f) => {
  1290. const head = elements[0];
  1291. const tail = elements.slice(1);
  1292. return f(universe, look, head, tail);
  1293. };
  1294. const oneAll = (universe, look, elements) => {
  1295. return elements.length > 0 ? all(universe, look, elements, unsafeOne) : Optional.none();
  1296. };
  1297. const unsafeOne = (universe, look, head, tail) => {
  1298. const start = look(universe, head);
  1299. return foldr(tail, (b, a) => {
  1300. const current = look(universe, a);
  1301. return commonElement(universe, b, current);
  1302. }, start);
  1303. };
  1304. const commonElement = (universe, start, end) => {
  1305. return start.bind(s => {
  1306. return end.filter(curry(universe.eq, s));
  1307. });
  1308. };
  1309. const sharedOne$1 = oneAll;
  1310. const universe = DomUniverse();
  1311. const sharedOne = (look, elements) => {
  1312. return sharedOne$1(universe, (_universe, element) => {
  1313. return look(element);
  1314. }, elements);
  1315. };
  1316. const lookupTable = container => {
  1317. return ancestor(container, 'table');
  1318. };
  1319. const retrieve$1 = (container, selector) => {
  1320. const sels = descendants(container, selector);
  1321. return sels.length > 0 ? Optional.some(sels) : Optional.none();
  1322. };
  1323. const getEdges = (container, firstSelectedSelector, lastSelectedSelector) => {
  1324. return descendant(container, firstSelectedSelector).bind(first => {
  1325. return descendant(container, lastSelectedSelector).bind(last => {
  1326. return sharedOne(lookupTable, [
  1327. first,
  1328. last
  1329. ]).map(table => {
  1330. return {
  1331. first,
  1332. last,
  1333. table
  1334. };
  1335. });
  1336. });
  1337. });
  1338. };
  1339. const retrieve = (container, selector) => {
  1340. return retrieve$1(container, selector);
  1341. };
  1342. const retrieveBox = (container, firstSelectedSelector, lastSelectedSelector) => {
  1343. return getEdges(container, firstSelectedSelector, lastSelectedSelector).bind(edges => {
  1344. const isRoot = ancestor => {
  1345. return eq(container, ancestor);
  1346. };
  1347. const sectionSelector = 'thead,tfoot,tbody,table';
  1348. const firstAncestor = ancestor(edges.first, sectionSelector, isRoot);
  1349. const lastAncestor = ancestor(edges.last, sectionSelector, isRoot);
  1350. return firstAncestor.bind(fA => {
  1351. return lastAncestor.bind(lA => {
  1352. return eq(fA, lA) ? getBox(edges.table, edges.first, edges.last) : Optional.none();
  1353. });
  1354. });
  1355. });
  1356. };
  1357. const fromDom = nodes => map(nodes, SugarElement.fromDom);
  1358. const strSelected = 'data-mce-selected';
  1359. const strSelectedSelector = 'td[' + strSelected + '],th[' + strSelected + ']';
  1360. const strFirstSelected = 'data-mce-first-selected';
  1361. const strFirstSelectedSelector = 'td[' + strFirstSelected + '],th[' + strFirstSelected + ']';
  1362. const strLastSelected = 'data-mce-last-selected';
  1363. const strLastSelectedSelector = 'td[' + strLastSelected + '],th[' + strLastSelected + ']';
  1364. const ephemera = {
  1365. selected: strSelected,
  1366. selectedSelector: strSelectedSelector,
  1367. firstSelected: strFirstSelected,
  1368. firstSelectedSelector: strFirstSelectedSelector,
  1369. lastSelected: strLastSelected,
  1370. lastSelectedSelector: strLastSelectedSelector
  1371. };
  1372. const getSelectionCellFallback = element => table(element).bind(table => retrieve(table, ephemera.firstSelectedSelector)).fold(constant(element), cells => cells[0]);
  1373. const getSelectionFromSelector = selector => (initCell, isRoot) => {
  1374. const cellName = name(initCell);
  1375. const cell = cellName === 'col' || cellName === 'colgroup' ? getSelectionCellFallback(initCell) : initCell;
  1376. return closest(cell, selector, isRoot);
  1377. };
  1378. const getSelectionCellOrCaption = getSelectionFromSelector('th,td,caption');
  1379. const getSelectionCell = getSelectionFromSelector('th,td');
  1380. const getCellsFromSelection = editor => fromDom(editor.model.table.getSelectedCells());
  1381. const getRowsFromSelection = (selected, selector) => {
  1382. const cellOpt = getSelectionCell(selected);
  1383. const rowsOpt = cellOpt.bind(cell => table(cell)).map(table => rows(table));
  1384. return lift2(cellOpt, rowsOpt, (cell, rows) => filter(rows, row => exists(fromDom(row.dom.cells), rowCell => get$2(rowCell, selector) === '1' || eq(rowCell, cell)))).getOr([]);
  1385. };
  1386. const verticalAlignValues = [
  1387. {
  1388. text: 'None',
  1389. value: ''
  1390. },
  1391. {
  1392. text: 'Top',
  1393. value: 'top'
  1394. },
  1395. {
  1396. text: 'Middle',
  1397. value: 'middle'
  1398. },
  1399. {
  1400. text: 'Bottom',
  1401. value: 'bottom'
  1402. }
  1403. ];
  1404. const hexColour = value => ({ value });
  1405. const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  1406. const longformRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
  1407. const isHexString = hex => shorthandRegex.test(hex) || longformRegex.test(hex);
  1408. const normalizeHex = hex => removeLeading(hex, '#').toUpperCase();
  1409. const fromString$1 = hex => isHexString(hex) ? Optional.some({ value: normalizeHex(hex) }) : Optional.none();
  1410. const toHex = component => {
  1411. const hex = component.toString(16);
  1412. return (hex.length === 1 ? '0' + hex : hex).toUpperCase();
  1413. };
  1414. const fromRgba = rgbaColour => {
  1415. const value = toHex(rgbaColour.red) + toHex(rgbaColour.green) + toHex(rgbaColour.blue);
  1416. return hexColour(value);
  1417. };
  1418. const rgbRegex = /^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$/i;
  1419. const rgbaRegex = /^\s*rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d?(?:\.\d+)?)\s*\)\s*$/i;
  1420. const rgbaColour = (red, green, blue, alpha) => ({
  1421. red,
  1422. green,
  1423. blue,
  1424. alpha
  1425. });
  1426. const fromStringValues = (red, green, blue, alpha) => {
  1427. const r = parseInt(red, 10);
  1428. const g = parseInt(green, 10);
  1429. const b = parseInt(blue, 10);
  1430. const a = parseFloat(alpha);
  1431. return rgbaColour(r, g, b, a);
  1432. };
  1433. const fromString = rgbaString => {
  1434. if (rgbaString === 'transparent') {
  1435. return Optional.some(rgbaColour(0, 0, 0, 0));
  1436. }
  1437. const rgbMatch = rgbRegex.exec(rgbaString);
  1438. if (rgbMatch !== null) {
  1439. return Optional.some(fromStringValues(rgbMatch[1], rgbMatch[2], rgbMatch[3], '1'));
  1440. }
  1441. const rgbaMatch = rgbaRegex.exec(rgbaString);
  1442. if (rgbaMatch !== null) {
  1443. return Optional.some(fromStringValues(rgbaMatch[1], rgbaMatch[2], rgbaMatch[3], rgbaMatch[4]));
  1444. }
  1445. return Optional.none();
  1446. };
  1447. const anyToHex = color => fromString$1(color).orThunk(() => fromString(color).map(fromRgba)).getOrThunk(() => {
  1448. const canvas = document.createElement('canvas');
  1449. canvas.height = 1;
  1450. canvas.width = 1;
  1451. const canvasContext = canvas.getContext('2d');
  1452. canvasContext.clearRect(0, 0, canvas.width, canvas.height);
  1453. canvasContext.fillStyle = '#FFFFFF';
  1454. canvasContext.fillStyle = color;
  1455. canvasContext.fillRect(0, 0, 1, 1);
  1456. const rgba = canvasContext.getImageData(0, 0, 1, 1).data;
  1457. const r = rgba[0];
  1458. const g = rgba[1];
  1459. const b = rgba[2];
  1460. const a = rgba[3];
  1461. return fromRgba(rgbaColour(r, g, b, a));
  1462. });
  1463. const rgbaToHexString = color => fromString(color).map(fromRgba).map(h => '#' + h.value).getOr(color);
  1464. const Cell = initial => {
  1465. let value = initial;
  1466. const get = () => {
  1467. return value;
  1468. };
  1469. const set = v => {
  1470. value = v;
  1471. };
  1472. return {
  1473. get,
  1474. set
  1475. };
  1476. };
  1477. const singleton = doRevoke => {
  1478. const subject = Cell(Optional.none());
  1479. const revoke = () => subject.get().each(doRevoke);
  1480. const clear = () => {
  1481. revoke();
  1482. subject.set(Optional.none());
  1483. };
  1484. const isSet = () => subject.get().isSome();
  1485. const get = () => subject.get();
  1486. const set = s => {
  1487. revoke();
  1488. subject.set(Optional.some(s));
  1489. };
  1490. return {
  1491. clear,
  1492. isSet,
  1493. get,
  1494. set
  1495. };
  1496. };
  1497. const unbindable = () => singleton(s => s.unbind());
  1498. const onSetupToggle = (editor, formatName, formatValue) => {
  1499. return api => {
  1500. const boundCallback = unbindable();
  1501. const isNone = isEmpty(formatValue);
  1502. const init = () => {
  1503. const selectedCells = getCellsFromSelection(editor);
  1504. const checkNode = cell => editor.formatter.match(formatName, { value: formatValue }, cell.dom, isNone);
  1505. if (isNone) {
  1506. api.setActive(!exists(selectedCells, checkNode));
  1507. boundCallback.set(editor.formatter.formatChanged(formatName, match => api.setActive(!match), true));
  1508. } else {
  1509. api.setActive(forall(selectedCells, checkNode));
  1510. boundCallback.set(editor.formatter.formatChanged(formatName, api.setActive, false, { value: formatValue }));
  1511. }
  1512. };
  1513. editor.initialized ? init() : editor.on('init', init);
  1514. return boundCallback.clear;
  1515. };
  1516. };
  1517. const isListGroup = item => hasNonNullableKey(item, 'menu');
  1518. const buildListItems = items => map(items, item => {
  1519. const text = item.text || item.title;
  1520. if (isListGroup(item)) {
  1521. return {
  1522. text,
  1523. items: buildListItems(item.menu)
  1524. };
  1525. } else {
  1526. return {
  1527. text,
  1528. value: item.value
  1529. };
  1530. }
  1531. });
  1532. const buildMenuItems = (editor, items, format, onAction) => map(items, item => {
  1533. const text = item.text || item.title;
  1534. if (isListGroup(item)) {
  1535. return {
  1536. type: 'nestedmenuitem',
  1537. text,
  1538. getSubmenuItems: () => buildMenuItems(editor, item.menu, format, onAction)
  1539. };
  1540. } else {
  1541. return {
  1542. text,
  1543. type: 'togglemenuitem',
  1544. onAction: () => onAction(item.value),
  1545. onSetup: onSetupToggle(editor, format, item.value)
  1546. };
  1547. }
  1548. });
  1549. const applyTableCellStyle = (editor, style) => value => {
  1550. editor.execCommand('mceTableApplyCellStyle', false, { [style]: value });
  1551. };
  1552. const filterNoneItem = list => bind(list, item => {
  1553. if (isListGroup(item)) {
  1554. return [{
  1555. ...item,
  1556. menu: filterNoneItem(item.menu)
  1557. }];
  1558. } else {
  1559. return isNotEmpty(item.value) ? [item] : [];
  1560. }
  1561. });
  1562. const generateMenuItemsCallback = (editor, items, format, onAction) => callback => callback(buildMenuItems(editor, items, format, onAction));
  1563. const buildColorMenu = (editor, colorList, style) => {
  1564. const colorMap = map(colorList, entry => ({
  1565. text: entry.title,
  1566. value: '#' + anyToHex(entry.value).value,
  1567. type: 'choiceitem'
  1568. }));
  1569. return [{
  1570. type: 'fancymenuitem',
  1571. fancytype: 'colorswatch',
  1572. initData: {
  1573. colors: colorMap.length > 0 ? colorMap : undefined,
  1574. allowCustomColors: false
  1575. },
  1576. onAction: data => {
  1577. const value = data.value === 'remove' ? '' : data.value;
  1578. editor.execCommand('mceTableApplyCellStyle', false, { [style]: value });
  1579. }
  1580. }];
  1581. };
  1582. const changeRowHeader = editor => () => {
  1583. const currentType = editor.queryCommandValue('mceTableRowType');
  1584. const newType = currentType === 'header' ? 'body' : 'header';
  1585. editor.execCommand('mceTableRowType', false, { type: newType });
  1586. };
  1587. const changeColumnHeader = editor => () => {
  1588. const currentType = editor.queryCommandValue('mceTableColType');
  1589. const newType = currentType === 'th' ? 'td' : 'th';
  1590. editor.execCommand('mceTableColType', false, { type: newType });
  1591. };
  1592. const getClassList$1 = editor => {
  1593. const classes = buildListItems(getCellClassList(editor));
  1594. if (classes.length > 0) {
  1595. return Optional.some({
  1596. name: 'class',
  1597. type: 'listbox',
  1598. label: 'Class',
  1599. items: classes
  1600. });
  1601. }
  1602. return Optional.none();
  1603. };
  1604. const children = [
  1605. {
  1606. name: 'width',
  1607. type: 'input',
  1608. label: 'Width'
  1609. },
  1610. {
  1611. name: 'height',
  1612. type: 'input',
  1613. label: 'Height'
  1614. },
  1615. {
  1616. name: 'celltype',
  1617. type: 'listbox',
  1618. label: 'Cell type',
  1619. items: [
  1620. {
  1621. text: 'Cell',
  1622. value: 'td'
  1623. },
  1624. {
  1625. text: 'Header cell',
  1626. value: 'th'
  1627. }
  1628. ]
  1629. },
  1630. {
  1631. name: 'scope',
  1632. type: 'listbox',
  1633. label: 'Scope',
  1634. items: [
  1635. {
  1636. text: 'None',
  1637. value: ''
  1638. },
  1639. {
  1640. text: 'Row',
  1641. value: 'row'
  1642. },
  1643. {
  1644. text: 'Column',
  1645. value: 'col'
  1646. },
  1647. {
  1648. text: 'Row group',
  1649. value: 'rowgroup'
  1650. },
  1651. {
  1652. text: 'Column group',
  1653. value: 'colgroup'
  1654. }
  1655. ]
  1656. },
  1657. {
  1658. name: 'halign',
  1659. type: 'listbox',
  1660. label: 'Horizontal align',
  1661. items: [
  1662. {
  1663. text: 'None',
  1664. value: ''
  1665. },
  1666. {
  1667. text: 'Left',
  1668. value: 'left'
  1669. },
  1670. {
  1671. text: 'Center',
  1672. value: 'center'
  1673. },
  1674. {
  1675. text: 'Right',
  1676. value: 'right'
  1677. }
  1678. ]
  1679. },
  1680. {
  1681. name: 'valign',
  1682. type: 'listbox',
  1683. label: 'Vertical align',
  1684. items: verticalAlignValues
  1685. }
  1686. ];
  1687. const getItems$2 = editor => children.concat(getClassList$1(editor).toArray());
  1688. const getAdvancedTab = (editor, dialogName) => {
  1689. const emptyBorderStyle = [{
  1690. text: 'Select...',
  1691. value: ''
  1692. }];
  1693. const advTabItems = [
  1694. {
  1695. name: 'borderstyle',
  1696. type: 'listbox',
  1697. label: 'Border style',
  1698. items: emptyBorderStyle.concat(buildListItems(getTableBorderStyles(editor)))
  1699. },
  1700. {
  1701. name: 'bordercolor',
  1702. type: 'colorinput',
  1703. label: 'Border color'
  1704. },
  1705. {
  1706. name: 'backgroundcolor',
  1707. type: 'colorinput',
  1708. label: 'Background color'
  1709. }
  1710. ];
  1711. const borderWidth = {
  1712. name: 'borderwidth',
  1713. type: 'input',
  1714. label: 'Border width'
  1715. };
  1716. const items = dialogName === 'cell' ? [borderWidth].concat(advTabItems) : advTabItems;
  1717. return {
  1718. title: 'Advanced',
  1719. name: 'advanced',
  1720. items
  1721. };
  1722. };
  1723. const normal = (editor, element) => {
  1724. const dom = editor.dom;
  1725. const setAttrib = (attr, value) => {
  1726. dom.setAttrib(element, attr, value);
  1727. };
  1728. const setStyle = (prop, value) => {
  1729. dom.setStyle(element, prop, value);
  1730. };
  1731. const setFormat = (formatName, value) => {
  1732. if (value === '') {
  1733. editor.formatter.remove(formatName, { value: null }, element, true);
  1734. } else {
  1735. editor.formatter.apply(formatName, { value }, element);
  1736. }
  1737. };
  1738. return {
  1739. setAttrib,
  1740. setStyle,
  1741. setFormat
  1742. };
  1743. };
  1744. const DomModifier = { normal };
  1745. const isHeaderCell = isTag('th');
  1746. const getRowHeaderType = (isHeaderRow, isHeaderCells) => {
  1747. if (isHeaderRow && isHeaderCells) {
  1748. return 'sectionCells';
  1749. } else if (isHeaderRow) {
  1750. return 'section';
  1751. } else {
  1752. return 'cells';
  1753. }
  1754. };
  1755. const getRowType$1 = row => {
  1756. const isHeaderRow = row.section === 'thead';
  1757. const isHeaderCells = is(findCommonCellType(row.cells), 'th');
  1758. if (row.section === 'tfoot') {
  1759. return { type: 'footer' };
  1760. } else if (isHeaderRow || isHeaderCells) {
  1761. return {
  1762. type: 'header',
  1763. subType: getRowHeaderType(isHeaderRow, isHeaderCells)
  1764. };
  1765. } else {
  1766. return { type: 'body' };
  1767. }
  1768. };
  1769. const findCommonCellType = cells => {
  1770. const headerCells = filter(cells, cell => isHeaderCell(cell.element));
  1771. if (headerCells.length === 0) {
  1772. return Optional.some('td');
  1773. } else if (headerCells.length === cells.length) {
  1774. return Optional.some('th');
  1775. } else {
  1776. return Optional.none();
  1777. }
  1778. };
  1779. const findCommonRowType = rows => {
  1780. const rowTypes = map(rows, row => getRowType$1(row).type);
  1781. const hasHeader = contains(rowTypes, 'header');
  1782. const hasFooter = contains(rowTypes, 'footer');
  1783. if (!hasHeader && !hasFooter) {
  1784. return Optional.some('body');
  1785. } else {
  1786. const hasBody = contains(rowTypes, 'body');
  1787. if (hasHeader && !hasBody && !hasFooter) {
  1788. return Optional.some('header');
  1789. } else if (!hasHeader && !hasBody && hasFooter) {
  1790. return Optional.some('footer');
  1791. } else {
  1792. return Optional.none();
  1793. }
  1794. }
  1795. };
  1796. const cached = f => {
  1797. let called = false;
  1798. let r;
  1799. return (...args) => {
  1800. if (!called) {
  1801. called = true;
  1802. r = f.apply(null, args);
  1803. }
  1804. return r;
  1805. };
  1806. };
  1807. const findInWarehouse = (warehouse, element) => findMap(warehouse.all, r => find(r.cells, e => eq(element, e.element)));
  1808. const extractCells = (warehouse, target, predicate) => {
  1809. const details = map(target.selection, cell$1 => {
  1810. return cell(cell$1).bind(lc => findInWarehouse(warehouse, lc)).filter(predicate);
  1811. });
  1812. const cells = cat(details);
  1813. return someIf(cells.length > 0, cells);
  1814. };
  1815. const onMergable = (_warehouse, target) => target.mergable;
  1816. const onUnmergable = (_warehouse, target) => target.unmergable;
  1817. const onCells = (warehouse, target) => extractCells(warehouse, target, always);
  1818. const isUnlockedTableCell = (warehouse, cell) => findInWarehouse(warehouse, cell).exists(detail => !detail.isLocked);
  1819. const allUnlocked = (warehouse, cells) => forall(cells, cell => isUnlockedTableCell(warehouse, cell));
  1820. const onUnlockedMergable = (warehouse, target) => onMergable(warehouse, target).filter(mergeable => allUnlocked(warehouse, mergeable.cells));
  1821. const onUnlockedUnmergable = (warehouse, target) => onUnmergable(warehouse, target).filter(cells => allUnlocked(warehouse, cells));
  1822. const generate = cases => {
  1823. if (!isArray(cases)) {
  1824. throw new Error('cases must be an array');
  1825. }
  1826. if (cases.length === 0) {
  1827. throw new Error('there must be at least one case');
  1828. }
  1829. const constructors = [];
  1830. const adt = {};
  1831. each(cases, (acase, count) => {
  1832. const keys$1 = keys(acase);
  1833. if (keys$1.length !== 1) {
  1834. throw new Error('one and only one name per case');
  1835. }
  1836. const key = keys$1[0];
  1837. const value = acase[key];
  1838. if (adt[key] !== undefined) {
  1839. throw new Error('duplicate key detected:' + key);
  1840. } else if (key === 'cata') {
  1841. throw new Error('cannot have a case named cata (sorry)');
  1842. } else if (!isArray(value)) {
  1843. throw new Error('case arguments must be an array');
  1844. }
  1845. constructors.push(key);
  1846. adt[key] = (...args) => {
  1847. const argLength = args.length;
  1848. if (argLength !== value.length) {
  1849. throw new Error('Wrong number of arguments to case ' + key + '. Expected ' + value.length + ' (' + value + '), got ' + argLength);
  1850. }
  1851. const match = branches => {
  1852. const branchKeys = keys(branches);
  1853. if (constructors.length !== branchKeys.length) {
  1854. throw new Error('Wrong number of arguments to match. Expected: ' + constructors.join(',') + '\nActual: ' + branchKeys.join(','));
  1855. }
  1856. const allReqd = forall(constructors, reqKey => {
  1857. return contains(branchKeys, reqKey);
  1858. });
  1859. if (!allReqd) {
  1860. throw new Error('Not all branches were specified when using match. Specified: ' + branchKeys.join(', ') + '\nRequired: ' + constructors.join(', '));
  1861. }
  1862. return branches[key].apply(null, args);
  1863. };
  1864. return {
  1865. fold: (...foldArgs) => {
  1866. if (foldArgs.length !== cases.length) {
  1867. throw new Error('Wrong number of arguments to fold. Expected ' + cases.length + ', got ' + foldArgs.length);
  1868. }
  1869. const target = foldArgs[count];
  1870. return target.apply(null, args);
  1871. },
  1872. match,
  1873. log: label => {
  1874. console.log(label, {
  1875. constructors,
  1876. constructor: key,
  1877. params: args
  1878. });
  1879. }
  1880. };
  1881. };
  1882. });
  1883. return adt;
  1884. };
  1885. const Adt = { generate };
  1886. const adt = Adt.generate([
  1887. { none: [] },
  1888. { only: ['index'] },
  1889. {
  1890. left: [
  1891. 'index',
  1892. 'next'
  1893. ]
  1894. },
  1895. {
  1896. middle: [
  1897. 'prev',
  1898. 'index',
  1899. 'next'
  1900. ]
  1901. },
  1902. {
  1903. right: [
  1904. 'prev',
  1905. 'index'
  1906. ]
  1907. }
  1908. ]);
  1909. ({ ...adt });
  1910. const opGetRowsType = (table, target) => {
  1911. const house = Warehouse.fromTable(table);
  1912. const details = onCells(house, target);
  1913. return details.bind(selectedCells => {
  1914. const lastSelectedCell = selectedCells[selectedCells.length - 1];
  1915. const minRowRange = selectedCells[0].row;
  1916. const maxRowRange = lastSelectedCell.row + lastSelectedCell.rowspan;
  1917. const selectedRows = house.all.slice(minRowRange, maxRowRange);
  1918. return findCommonRowType(selectedRows);
  1919. }).getOr('');
  1920. };
  1921. const getRowsType = opGetRowsType;
  1922. const rgbToHex = value => startsWith(value, 'rgb') ? rgbaToHexString(value) : value;
  1923. const extractAdvancedStyles = elm => {
  1924. const element = SugarElement.fromDom(elm);
  1925. return {
  1926. borderwidth: getRaw(element, 'border-width').getOr(''),
  1927. borderstyle: getRaw(element, 'border-style').getOr(''),
  1928. bordercolor: getRaw(element, 'border-color').map(rgbToHex).getOr(''),
  1929. backgroundcolor: getRaw(element, 'background-color').map(rgbToHex).getOr('')
  1930. };
  1931. };
  1932. const getSharedValues = data => {
  1933. const baseData = data[0];
  1934. const comparisonData = data.slice(1);
  1935. each(comparisonData, items => {
  1936. each(keys(baseData), key => {
  1937. each$1(items, (itemValue, itemKey) => {
  1938. const comparisonValue = baseData[key];
  1939. if (comparisonValue !== '' && key === itemKey) {
  1940. if (comparisonValue !== itemValue) {
  1941. baseData[key] = '';
  1942. }
  1943. }
  1944. });
  1945. });
  1946. });
  1947. return baseData;
  1948. };
  1949. const getAlignment = (formats, formatName, editor, elm) => find(formats, name => !isUndefined(editor.formatter.matchNode(elm, formatName + name))).getOr('');
  1950. const getHAlignment = curry(getAlignment, [
  1951. 'left',
  1952. 'center',
  1953. 'right'
  1954. ], 'align');
  1955. const getVAlignment = curry(getAlignment, [
  1956. 'top',
  1957. 'middle',
  1958. 'bottom'
  1959. ], 'valign');
  1960. const extractDataFromSettings = (editor, hasAdvTableTab) => {
  1961. const style = getDefaultStyles(editor);
  1962. const attrs = getDefaultAttributes(editor);
  1963. const extractAdvancedStyleData = () => ({
  1964. borderstyle: get$4(style, 'border-style').getOr(''),
  1965. bordercolor: rgbToHex(get$4(style, 'border-color').getOr('')),
  1966. backgroundcolor: rgbToHex(get$4(style, 'background-color').getOr(''))
  1967. });
  1968. const defaultData = {
  1969. height: '',
  1970. width: '100%',
  1971. cellspacing: '',
  1972. cellpadding: '',
  1973. caption: false,
  1974. class: '',
  1975. align: '',
  1976. border: ''
  1977. };
  1978. const getBorder = () => {
  1979. const borderWidth = style['border-width'];
  1980. if (shouldStyleWithCss(editor) && borderWidth) {
  1981. return { border: borderWidth };
  1982. }
  1983. return get$4(attrs, 'border').fold(() => ({}), border => ({ border }));
  1984. };
  1985. const advStyle = hasAdvTableTab ? extractAdvancedStyleData() : {};
  1986. const getCellPaddingCellSpacing = () => {
  1987. const spacing = get$4(style, 'border-spacing').or(get$4(attrs, 'cellspacing')).fold(() => ({}), cellspacing => ({ cellspacing }));
  1988. const padding = get$4(style, 'border-padding').or(get$4(attrs, 'cellpadding')).fold(() => ({}), cellpadding => ({ cellpadding }));
  1989. return {
  1990. ...spacing,
  1991. ...padding
  1992. };
  1993. };
  1994. const data = {
  1995. ...defaultData,
  1996. ...style,
  1997. ...attrs,
  1998. ...advStyle,
  1999. ...getBorder(),
  2000. ...getCellPaddingCellSpacing()
  2001. };
  2002. return data;
  2003. };
  2004. const getRowType = elm => table(SugarElement.fromDom(elm)).map(table => {
  2005. const target = { selection: fromDom(elm.cells) };
  2006. return getRowsType(table, target);
  2007. }).getOr('');
  2008. const extractDataFromTableElement = (editor, elm, hasAdvTableTab) => {
  2009. const getBorder = (dom, elm) => {
  2010. const optBorderWidth = getRaw(SugarElement.fromDom(elm), 'border-width');
  2011. if (shouldStyleWithCss(editor) && optBorderWidth.isSome()) {
  2012. return optBorderWidth.getOr('');
  2013. }
  2014. return dom.getAttrib(elm, 'border') || getTDTHOverallStyle(editor.dom, elm, 'border-width') || getTDTHOverallStyle(editor.dom, elm, 'border');
  2015. };
  2016. const dom = editor.dom;
  2017. const cellspacing = shouldStyleWithCss(editor) ? dom.getStyle(elm, 'border-spacing') || dom.getAttrib(elm, 'cellspacing') : dom.getAttrib(elm, 'cellspacing') || dom.getStyle(elm, 'border-spacing');
  2018. const cellpadding = shouldStyleWithCss(editor) ? getTDTHOverallStyle(dom, elm, 'padding') || dom.getAttrib(elm, 'cellpadding') : dom.getAttrib(elm, 'cellpadding') || getTDTHOverallStyle(dom, elm, 'padding');
  2019. return {
  2020. width: dom.getStyle(elm, 'width') || dom.getAttrib(elm, 'width'),
  2021. height: dom.getStyle(elm, 'height') || dom.getAttrib(elm, 'height'),
  2022. cellspacing,
  2023. cellpadding,
  2024. border: getBorder(dom, elm),
  2025. caption: !!dom.select('caption', elm)[0],
  2026. class: dom.getAttrib(elm, 'class', ''),
  2027. align: getHAlignment(editor, elm),
  2028. ...hasAdvTableTab ? extractAdvancedStyles(elm) : {}
  2029. };
  2030. };
  2031. const extractDataFromRowElement = (editor, elm, hasAdvancedRowTab) => {
  2032. const dom = editor.dom;
  2033. return {
  2034. height: dom.getStyle(elm, 'height') || dom.getAttrib(elm, 'height'),
  2035. class: dom.getAttrib(elm, 'class', ''),
  2036. type: getRowType(elm),
  2037. align: getHAlignment(editor, elm),
  2038. ...hasAdvancedRowTab ? extractAdvancedStyles(elm) : {}
  2039. };
  2040. };
  2041. const extractDataFromCellElement = (editor, cell, hasAdvancedCellTab, column) => {
  2042. const dom = editor.dom;
  2043. const colElm = column.getOr(cell);
  2044. const getStyle = (element, style) => dom.getStyle(element, style) || dom.getAttrib(element, style);
  2045. return {
  2046. width: getStyle(colElm, 'width'),
  2047. height: getStyle(cell, 'height'),
  2048. scope: dom.getAttrib(cell, 'scope'),
  2049. celltype: getNodeName(cell),
  2050. class: dom.getAttrib(cell, 'class', ''),
  2051. halign: getHAlignment(editor, cell),
  2052. valign: getVAlignment(editor, cell),
  2053. ...hasAdvancedCellTab ? extractAdvancedStyles(cell) : {}
  2054. };
  2055. };
  2056. const getSelectedCells = (table, cells) => {
  2057. const warehouse = Warehouse.fromTable(table);
  2058. const allCells = Warehouse.justCells(warehouse);
  2059. const filtered = filter(allCells, cellA => exists(cells, cellB => eq(cellA.element, cellB)));
  2060. return map(filtered, cell => ({
  2061. element: cell.element.dom,
  2062. column: Warehouse.getColumnAt(warehouse, cell.column).map(col => col.element.dom)
  2063. }));
  2064. };
  2065. const updateSimpleProps$1 = (modifier, colModifier, data, shouldUpdate) => {
  2066. if (shouldUpdate('scope')) {
  2067. modifier.setAttrib('scope', data.scope);
  2068. }
  2069. if (shouldUpdate('class')) {
  2070. modifier.setAttrib('class', data.class);
  2071. }
  2072. if (shouldUpdate('height')) {
  2073. modifier.setStyle('height', addPxSuffix(data.height));
  2074. }
  2075. if (shouldUpdate('width')) {
  2076. colModifier.setStyle('width', addPxSuffix(data.width));
  2077. }
  2078. };
  2079. const updateAdvancedProps$1 = (modifier, data, shouldUpdate) => {
  2080. if (shouldUpdate('backgroundcolor')) {
  2081. modifier.setFormat('tablecellbackgroundcolor', data.backgroundcolor);
  2082. }
  2083. if (shouldUpdate('bordercolor')) {
  2084. modifier.setFormat('tablecellbordercolor', data.bordercolor);
  2085. }
  2086. if (shouldUpdate('borderstyle')) {
  2087. modifier.setFormat('tablecellborderstyle', data.borderstyle);
  2088. }
  2089. if (shouldUpdate('borderwidth')) {
  2090. modifier.setFormat('tablecellborderwidth', addPxSuffix(data.borderwidth));
  2091. }
  2092. };
  2093. const applyStyleData$1 = (editor, cells, data, wasChanged) => {
  2094. const isSingleCell = cells.length === 1;
  2095. each(cells, item => {
  2096. const cellElm = item.element;
  2097. const shouldOverrideCurrentValue = isSingleCell ? always : wasChanged;
  2098. const modifier = DomModifier.normal(editor, cellElm);
  2099. const colModifier = item.column.map(col => DomModifier.normal(editor, col)).getOr(modifier);
  2100. updateSimpleProps$1(modifier, colModifier, data, shouldOverrideCurrentValue);
  2101. if (hasAdvancedCellTab(editor)) {
  2102. updateAdvancedProps$1(modifier, data, shouldOverrideCurrentValue);
  2103. }
  2104. if (wasChanged('halign')) {
  2105. setAlign(editor, cellElm, data.halign);
  2106. }
  2107. if (wasChanged('valign')) {
  2108. setVAlign(editor, cellElm, data.valign);
  2109. }
  2110. });
  2111. };
  2112. const applyStructureData$1 = (editor, data) => {
  2113. editor.execCommand('mceTableCellType', false, {
  2114. type: data.celltype,
  2115. no_events: true
  2116. });
  2117. };
  2118. const applyCellData = (editor, cells, oldData, data) => {
  2119. const modifiedData = filter$1(data, (value, key) => oldData[key] !== value);
  2120. if (size(modifiedData) > 0 && cells.length >= 1) {
  2121. table(cells[0]).each(table => {
  2122. const selectedCells = getSelectedCells(table, cells);
  2123. const styleModified = size(filter$1(modifiedData, (_value, key) => key !== 'scope' && key !== 'celltype')) > 0;
  2124. const structureModified = has(modifiedData, 'celltype');
  2125. if (styleModified || has(modifiedData, 'scope')) {
  2126. applyStyleData$1(editor, selectedCells, data, curry(has, modifiedData));
  2127. }
  2128. if (structureModified) {
  2129. applyStructureData$1(editor, data);
  2130. }
  2131. fireTableModified(editor, table.dom, {
  2132. structure: structureModified,
  2133. style: styleModified
  2134. });
  2135. });
  2136. }
  2137. };
  2138. const onSubmitCellForm = (editor, cells, oldData, api) => {
  2139. const data = api.getData();
  2140. api.close();
  2141. editor.undoManager.transact(() => {
  2142. applyCellData(editor, cells, oldData, data);
  2143. editor.focus();
  2144. });
  2145. };
  2146. const getData$1 = (editor, cells) => {
  2147. const cellsData = table(cells[0]).map(table => map(getSelectedCells(table, cells), item => extractDataFromCellElement(editor, item.element, hasAdvancedCellTab(editor), item.column)));
  2148. return getSharedValues(cellsData.getOrDie());
  2149. };
  2150. const open$2 = editor => {
  2151. const cells = getCellsFromSelection(editor);
  2152. if (cells.length === 0) {
  2153. return;
  2154. }
  2155. const data = getData$1(editor, cells);
  2156. const dialogTabPanel = {
  2157. type: 'tabpanel',
  2158. tabs: [
  2159. {
  2160. title: 'General',
  2161. name: 'general',
  2162. items: getItems$2(editor)
  2163. },
  2164. getAdvancedTab(editor, 'cell')
  2165. ]
  2166. };
  2167. const dialogPanel = {
  2168. type: 'panel',
  2169. items: [{
  2170. type: 'grid',
  2171. columns: 2,
  2172. items: getItems$2(editor)
  2173. }]
  2174. };
  2175. editor.windowManager.open({
  2176. title: 'Cell Properties',
  2177. size: 'normal',
  2178. body: hasAdvancedCellTab(editor) ? dialogTabPanel : dialogPanel,
  2179. buttons: [
  2180. {
  2181. type: 'cancel',
  2182. name: 'cancel',
  2183. text: 'Cancel'
  2184. },
  2185. {
  2186. type: 'submit',
  2187. name: 'save',
  2188. text: 'Save',
  2189. primary: true
  2190. }
  2191. ],
  2192. initialData: data,
  2193. onSubmit: curry(onSubmitCellForm, editor, cells, data)
  2194. });
  2195. };
  2196. const getClassList = editor => {
  2197. const classes = buildListItems(getRowClassList(editor));
  2198. if (classes.length > 0) {
  2199. return Optional.some({
  2200. name: 'class',
  2201. type: 'listbox',
  2202. label: 'Class',
  2203. items: classes
  2204. });
  2205. }
  2206. return Optional.none();
  2207. };
  2208. const formChildren = [
  2209. {
  2210. type: 'listbox',
  2211. name: 'type',
  2212. label: 'Row type',
  2213. items: [
  2214. {
  2215. text: 'Header',
  2216. value: 'header'
  2217. },
  2218. {
  2219. text: 'Body',
  2220. value: 'body'
  2221. },
  2222. {
  2223. text: 'Footer',
  2224. value: 'footer'
  2225. }
  2226. ]
  2227. },
  2228. {
  2229. type: 'listbox',
  2230. name: 'align',
  2231. label: 'Alignment',
  2232. items: [
  2233. {
  2234. text: 'None',
  2235. value: ''
  2236. },
  2237. {
  2238. text: 'Left',
  2239. value: 'left'
  2240. },
  2241. {
  2242. text: 'Center',
  2243. value: 'center'
  2244. },
  2245. {
  2246. text: 'Right',
  2247. value: 'right'
  2248. }
  2249. ]
  2250. },
  2251. {
  2252. label: 'Height',
  2253. name: 'height',
  2254. type: 'input'
  2255. }
  2256. ];
  2257. const getItems$1 = editor => formChildren.concat(getClassList(editor).toArray());
  2258. const updateSimpleProps = (modifier, data, shouldUpdate) => {
  2259. if (shouldUpdate('class')) {
  2260. modifier.setAttrib('class', data.class);
  2261. }
  2262. if (shouldUpdate('height')) {
  2263. modifier.setStyle('height', addPxSuffix(data.height));
  2264. }
  2265. };
  2266. const updateAdvancedProps = (modifier, data, shouldUpdate) => {
  2267. if (shouldUpdate('backgroundcolor')) {
  2268. modifier.setStyle('background-color', data.backgroundcolor);
  2269. }
  2270. if (shouldUpdate('bordercolor')) {
  2271. modifier.setStyle('border-color', data.bordercolor);
  2272. }
  2273. if (shouldUpdate('borderstyle')) {
  2274. modifier.setStyle('border-style', data.borderstyle);
  2275. }
  2276. };
  2277. const applyStyleData = (editor, rows, data, wasChanged) => {
  2278. const isSingleRow = rows.length === 1;
  2279. const shouldOverrideCurrentValue = isSingleRow ? always : wasChanged;
  2280. each(rows, rowElm => {
  2281. const modifier = DomModifier.normal(editor, rowElm);
  2282. updateSimpleProps(modifier, data, shouldOverrideCurrentValue);
  2283. if (hasAdvancedRowTab(editor)) {
  2284. updateAdvancedProps(modifier, data, shouldOverrideCurrentValue);
  2285. }
  2286. if (wasChanged('align')) {
  2287. setAlign(editor, rowElm, data.align);
  2288. }
  2289. });
  2290. };
  2291. const applyStructureData = (editor, data) => {
  2292. editor.execCommand('mceTableRowType', false, {
  2293. type: data.type,
  2294. no_events: true
  2295. });
  2296. };
  2297. const applyRowData = (editor, rows, oldData, data) => {
  2298. const modifiedData = filter$1(data, (value, key) => oldData[key] !== value);
  2299. if (size(modifiedData) > 0) {
  2300. const typeModified = has(modifiedData, 'type');
  2301. const styleModified = typeModified ? size(modifiedData) > 1 : true;
  2302. if (styleModified) {
  2303. applyStyleData(editor, rows, data, curry(has, modifiedData));
  2304. }
  2305. if (typeModified) {
  2306. applyStructureData(editor, data);
  2307. }
  2308. table(SugarElement.fromDom(rows[0])).each(table => fireTableModified(editor, table.dom, {
  2309. structure: typeModified,
  2310. style: styleModified
  2311. }));
  2312. }
  2313. };
  2314. const onSubmitRowForm = (editor, rows, oldData, api) => {
  2315. const data = api.getData();
  2316. api.close();
  2317. editor.undoManager.transact(() => {
  2318. applyRowData(editor, rows, oldData, data);
  2319. editor.focus();
  2320. });
  2321. };
  2322. const open$1 = editor => {
  2323. const rows = getRowsFromSelection(getSelectionStart(editor), ephemera.selected);
  2324. if (rows.length === 0) {
  2325. return;
  2326. }
  2327. const rowsData = map(rows, rowElm => extractDataFromRowElement(editor, rowElm.dom, hasAdvancedRowTab(editor)));
  2328. const data = getSharedValues(rowsData);
  2329. const dialogTabPanel = {
  2330. type: 'tabpanel',
  2331. tabs: [
  2332. {
  2333. title: 'General',
  2334. name: 'general',
  2335. items: getItems$1(editor)
  2336. },
  2337. getAdvancedTab(editor, 'row')
  2338. ]
  2339. };
  2340. const dialogPanel = {
  2341. type: 'panel',
  2342. items: [{
  2343. type: 'grid',
  2344. columns: 2,
  2345. items: getItems$1(editor)
  2346. }]
  2347. };
  2348. editor.windowManager.open({
  2349. title: 'Row Properties',
  2350. size: 'normal',
  2351. body: hasAdvancedRowTab(editor) ? dialogTabPanel : dialogPanel,
  2352. buttons: [
  2353. {
  2354. type: 'cancel',
  2355. name: 'cancel',
  2356. text: 'Cancel'
  2357. },
  2358. {
  2359. type: 'submit',
  2360. name: 'save',
  2361. text: 'Save',
  2362. primary: true
  2363. }
  2364. ],
  2365. initialData: data,
  2366. onSubmit: curry(onSubmitRowForm, editor, map(rows, r => r.dom), data)
  2367. });
  2368. };
  2369. const getItems = (editor, classes, insertNewTable) => {
  2370. const rowColCountItems = !insertNewTable ? [] : [
  2371. {
  2372. type: 'input',
  2373. name: 'cols',
  2374. label: 'Cols',
  2375. inputMode: 'numeric'
  2376. },
  2377. {
  2378. type: 'input',
  2379. name: 'rows',
  2380. label: 'Rows',
  2381. inputMode: 'numeric'
  2382. }
  2383. ];
  2384. const alwaysItems = [
  2385. {
  2386. type: 'input',
  2387. name: 'width',
  2388. label: 'Width'
  2389. },
  2390. {
  2391. type: 'input',
  2392. name: 'height',
  2393. label: 'Height'
  2394. }
  2395. ];
  2396. const appearanceItems = hasAppearanceOptions(editor) ? [
  2397. {
  2398. type: 'input',
  2399. name: 'cellspacing',
  2400. label: 'Cell spacing',
  2401. inputMode: 'numeric'
  2402. },
  2403. {
  2404. type: 'input',
  2405. name: 'cellpadding',
  2406. label: 'Cell padding',
  2407. inputMode: 'numeric'
  2408. },
  2409. {
  2410. type: 'input',
  2411. name: 'border',
  2412. label: 'Border width'
  2413. },
  2414. {
  2415. type: 'label',
  2416. label: 'Caption',
  2417. items: [{
  2418. type: 'checkbox',
  2419. name: 'caption',
  2420. label: 'Show caption'
  2421. }]
  2422. }
  2423. ] : [];
  2424. const alignmentItem = [{
  2425. type: 'listbox',
  2426. name: 'align',
  2427. label: 'Alignment',
  2428. items: [
  2429. {
  2430. text: 'None',
  2431. value: ''
  2432. },
  2433. {
  2434. text: 'Left',
  2435. value: 'left'
  2436. },
  2437. {
  2438. text: 'Center',
  2439. value: 'center'
  2440. },
  2441. {
  2442. text: 'Right',
  2443. value: 'right'
  2444. }
  2445. ]
  2446. }];
  2447. const classListItem = classes.length > 0 ? [{
  2448. type: 'listbox',
  2449. name: 'class',
  2450. label: 'Class',
  2451. items: classes
  2452. }] : [];
  2453. return rowColCountItems.concat(alwaysItems).concat(appearanceItems).concat(alignmentItem).concat(classListItem);
  2454. };
  2455. const styleTDTH = (dom, elm, name, value) => {
  2456. if (elm.tagName === 'TD' || elm.tagName === 'TH') {
  2457. if (isString(name)) {
  2458. dom.setStyle(elm, name, value);
  2459. } else {
  2460. dom.setStyles(elm, name);
  2461. }
  2462. } else {
  2463. if (elm.children) {
  2464. for (let i = 0; i < elm.children.length; i++) {
  2465. styleTDTH(dom, elm.children[i], name, value);
  2466. }
  2467. }
  2468. }
  2469. };
  2470. const applyDataToElement = (editor, tableElm, data) => {
  2471. const dom = editor.dom;
  2472. const attrs = {};
  2473. const styles = {};
  2474. attrs.class = data.class;
  2475. styles.height = addPxSuffix(data.height);
  2476. if (shouldStyleWithCss(editor)) {
  2477. styles.width = addPxSuffix(data.width);
  2478. } else if (dom.getAttrib(tableElm, 'width')) {
  2479. attrs.width = removePxSuffix(data.width);
  2480. }
  2481. if (shouldStyleWithCss(editor)) {
  2482. styles['border-width'] = addPxSuffix(data.border);
  2483. styles['border-spacing'] = addPxSuffix(data.cellspacing);
  2484. } else {
  2485. attrs.border = data.border;
  2486. attrs.cellpadding = data.cellpadding;
  2487. attrs.cellspacing = data.cellspacing;
  2488. }
  2489. if (shouldStyleWithCss(editor) && tableElm.children) {
  2490. for (let i = 0; i < tableElm.children.length; i++) {
  2491. styleTDTH(dom, tableElm.children[i], {
  2492. 'border-width': addPxSuffix(data.border),
  2493. 'padding': addPxSuffix(data.cellpadding)
  2494. });
  2495. if (hasAdvancedTableTab(editor)) {
  2496. styleTDTH(dom, tableElm.children[i], { 'border-color': data.bordercolor });
  2497. }
  2498. }
  2499. }
  2500. if (hasAdvancedTableTab(editor)) {
  2501. styles['background-color'] = data.backgroundcolor;
  2502. styles['border-color'] = data.bordercolor;
  2503. styles['border-style'] = data.borderstyle;
  2504. }
  2505. attrs.style = dom.serializeStyle({
  2506. ...getDefaultStyles(editor),
  2507. ...styles
  2508. });
  2509. dom.setAttribs(tableElm, {
  2510. ...getDefaultAttributes(editor),
  2511. ...attrs
  2512. });
  2513. };
  2514. const onSubmitTableForm = (editor, tableElm, oldData, api) => {
  2515. const dom = editor.dom;
  2516. const data = api.getData();
  2517. const modifiedData = filter$1(data, (value, key) => oldData[key] !== value);
  2518. api.close();
  2519. if (data.class === '') {
  2520. delete data.class;
  2521. }
  2522. editor.undoManager.transact(() => {
  2523. if (!tableElm) {
  2524. const cols = parseInt(data.cols, 10) || 1;
  2525. const rows = parseInt(data.rows, 10) || 1;
  2526. editor.execCommand('mceInsertTable', false, {
  2527. rows,
  2528. columns: cols
  2529. });
  2530. tableElm = getSelectionCell(getSelectionStart(editor), getIsRoot(editor)).bind(cell => table(cell, getIsRoot(editor))).map(table => table.dom).getOrUndefined();
  2531. }
  2532. if (size(modifiedData) > 0) {
  2533. applyDataToElement(editor, tableElm, data);
  2534. const captionElm = dom.select('caption', tableElm)[0];
  2535. if (captionElm && !data.caption || !captionElm && data.caption) {
  2536. editor.execCommand('mceTableToggleCaption');
  2537. }
  2538. setAlign(editor, tableElm, data.align);
  2539. }
  2540. editor.focus();
  2541. editor.addVisual();
  2542. if (size(modifiedData) > 0) {
  2543. const captionModified = has(modifiedData, 'caption');
  2544. const styleModified = captionModified ? size(modifiedData) > 1 : true;
  2545. fireTableModified(editor, tableElm, {
  2546. structure: captionModified,
  2547. style: styleModified
  2548. });
  2549. }
  2550. });
  2551. };
  2552. const open = (editor, insertNewTable) => {
  2553. const dom = editor.dom;
  2554. let tableElm;
  2555. let data = extractDataFromSettings(editor, hasAdvancedTableTab(editor));
  2556. if (insertNewTable === false) {
  2557. tableElm = dom.getParent(editor.selection.getStart(), 'table', editor.getBody());
  2558. if (tableElm) {
  2559. data = extractDataFromTableElement(editor, tableElm, hasAdvancedTableTab(editor));
  2560. } else {
  2561. if (hasAdvancedTableTab(editor)) {
  2562. data.borderstyle = '';
  2563. data.bordercolor = '';
  2564. data.backgroundcolor = '';
  2565. }
  2566. }
  2567. } else {
  2568. data.cols = '1';
  2569. data.rows = '1';
  2570. if (hasAdvancedTableTab(editor)) {
  2571. data.borderstyle = '';
  2572. data.bordercolor = '';
  2573. data.backgroundcolor = '';
  2574. }
  2575. }
  2576. const classes = buildListItems(getTableClassList(editor));
  2577. if (classes.length > 0) {
  2578. if (data.class) {
  2579. data.class = data.class.replace(/\s*mce\-item\-table\s*/g, '');
  2580. }
  2581. }
  2582. const generalPanel = {
  2583. type: 'grid',
  2584. columns: 2,
  2585. items: getItems(editor, classes, insertNewTable)
  2586. };
  2587. const nonAdvancedForm = () => ({
  2588. type: 'panel',
  2589. items: [generalPanel]
  2590. });
  2591. const advancedForm = () => ({
  2592. type: 'tabpanel',
  2593. tabs: [
  2594. {
  2595. title: 'General',
  2596. name: 'general',
  2597. items: [generalPanel]
  2598. },
  2599. getAdvancedTab(editor, 'table')
  2600. ]
  2601. });
  2602. const dialogBody = hasAdvancedTableTab(editor) ? advancedForm() : nonAdvancedForm();
  2603. editor.windowManager.open({
  2604. title: 'Table Properties',
  2605. size: 'normal',
  2606. body: dialogBody,
  2607. onSubmit: curry(onSubmitTableForm, editor, tableElm, data),
  2608. buttons: [
  2609. {
  2610. type: 'cancel',
  2611. name: 'cancel',
  2612. text: 'Cancel'
  2613. },
  2614. {
  2615. type: 'submit',
  2616. name: 'save',
  2617. text: 'Save',
  2618. primary: true
  2619. }
  2620. ],
  2621. initialData: data
  2622. });
  2623. };
  2624. const registerCommands = editor => {
  2625. each$1({
  2626. mceTableProps: curry(open, editor, false),
  2627. mceTableRowProps: curry(open$1, editor),
  2628. mceTableCellProps: curry(open$2, editor)
  2629. }, (func, name) => editor.addCommand(name, () => func()));
  2630. editor.addCommand('mceInsertTableDialog', _ui => {
  2631. open(editor, true);
  2632. });
  2633. };
  2634. const child = (scope, selector) => child$1(scope, selector).isSome();
  2635. const selection = identity;
  2636. const unmergable = selectedCells => {
  2637. const hasSpan = (elem, type) => getOpt(elem, type).exists(span => parseInt(span, 10) > 1);
  2638. const hasRowOrColSpan = elem => hasSpan(elem, 'rowspan') || hasSpan(elem, 'colspan');
  2639. return selectedCells.length > 0 && forall(selectedCells, hasRowOrColSpan) ? Optional.some(selectedCells) : Optional.none();
  2640. };
  2641. const mergable = (table, selectedCells, ephemera) => {
  2642. if (selectedCells.length <= 1) {
  2643. return Optional.none();
  2644. } else {
  2645. return retrieveBox(table, ephemera.firstSelectedSelector, ephemera.lastSelectedSelector).map(bounds => ({
  2646. bounds,
  2647. cells: selectedCells
  2648. }));
  2649. }
  2650. };
  2651. const noMenu = cell => ({
  2652. element: cell,
  2653. mergable: Optional.none(),
  2654. unmergable: Optional.none(),
  2655. selection: [cell]
  2656. });
  2657. const forMenu = (selectedCells, table, cell) => ({
  2658. element: cell,
  2659. mergable: mergable(table, selectedCells, ephemera),
  2660. unmergable: unmergable(selectedCells),
  2661. selection: selection(selectedCells)
  2662. });
  2663. const getSelectionTargets = editor => {
  2664. const targets = Cell(Optional.none());
  2665. const changeHandlers = Cell([]);
  2666. let selectionDetails = Optional.none();
  2667. const isCaption = isTag('caption');
  2668. const isDisabledForSelection = key => selectionDetails.forall(details => !details[key]);
  2669. const getStart = () => getSelectionCellOrCaption(getSelectionStart(editor), getIsRoot(editor));
  2670. const getEnd = () => getSelectionCellOrCaption(getSelectionEnd(editor), getIsRoot(editor));
  2671. const findTargets = () => getStart().bind(startCellOrCaption => flatten(lift2(table(startCellOrCaption), getEnd().bind(table), (startTable, endTable) => {
  2672. if (eq(startTable, endTable)) {
  2673. if (isCaption(startCellOrCaption)) {
  2674. return Optional.some(noMenu(startCellOrCaption));
  2675. } else {
  2676. return Optional.some(forMenu(getCellsFromSelection(editor), startTable, startCellOrCaption));
  2677. }
  2678. }
  2679. return Optional.none();
  2680. })));
  2681. const getExtractedDetails = targets => {
  2682. const tableOpt = table(targets.element);
  2683. return tableOpt.map(table => {
  2684. const warehouse = Warehouse.fromTable(table);
  2685. const selectedCells = onCells(warehouse, targets).getOr([]);
  2686. const locked = foldl(selectedCells, (acc, cell) => {
  2687. if (cell.isLocked) {
  2688. acc.onAny = true;
  2689. if (cell.column === 0) {
  2690. acc.onFirst = true;
  2691. } else if (cell.column + cell.colspan >= warehouse.grid.columns) {
  2692. acc.onLast = true;
  2693. }
  2694. }
  2695. return acc;
  2696. }, {
  2697. onAny: false,
  2698. onFirst: false,
  2699. onLast: false
  2700. });
  2701. return {
  2702. mergeable: onUnlockedMergable(warehouse, targets).isSome(),
  2703. unmergeable: onUnlockedUnmergable(warehouse, targets).isSome(),
  2704. locked
  2705. };
  2706. });
  2707. };
  2708. const resetTargets = () => {
  2709. targets.set(cached(findTargets)());
  2710. selectionDetails = targets.get().bind(getExtractedDetails);
  2711. each(changeHandlers.get(), handler => handler());
  2712. };
  2713. const setupHandler = handler => {
  2714. handler();
  2715. changeHandlers.set(changeHandlers.get().concat([handler]));
  2716. return () => {
  2717. changeHandlers.set(filter(changeHandlers.get(), h => h !== handler));
  2718. };
  2719. };
  2720. const onSetup = (api, isDisabled) => setupHandler(() => targets.get().fold(() => {
  2721. api.setEnabled(false);
  2722. }, targets => {
  2723. api.setEnabled(!isDisabled(targets));
  2724. }));
  2725. const onSetupWithToggle = (api, isDisabled, isActive) => setupHandler(() => targets.get().fold(() => {
  2726. api.setEnabled(false);
  2727. api.setActive(false);
  2728. }, targets => {
  2729. api.setEnabled(!isDisabled(targets));
  2730. api.setActive(isActive(targets));
  2731. }));
  2732. const isDisabledFromLocked = lockedDisable => selectionDetails.exists(details => details.locked[lockedDisable]);
  2733. const onSetupTable = api => onSetup(api, _ => false);
  2734. const onSetupCellOrRow = api => onSetup(api, targets => isCaption(targets.element));
  2735. const onSetupColumn = lockedDisable => api => onSetup(api, targets => isCaption(targets.element) || isDisabledFromLocked(lockedDisable));
  2736. const onSetupPasteable = getClipboardData => api => onSetup(api, targets => isCaption(targets.element) || getClipboardData().isNone());
  2737. const onSetupPasteableColumn = (getClipboardData, lockedDisable) => api => onSetup(api, targets => isCaption(targets.element) || getClipboardData().isNone() || isDisabledFromLocked(lockedDisable));
  2738. const onSetupMergeable = api => onSetup(api, _targets => isDisabledForSelection('mergeable'));
  2739. const onSetupUnmergeable = api => onSetup(api, _targets => isDisabledForSelection('unmergeable'));
  2740. const onSetupTableWithCaption = api => {
  2741. return onSetupWithToggle(api, never, targets => {
  2742. const tableOpt = table(targets.element, getIsRoot(editor));
  2743. return tableOpt.exists(table => child(table, 'caption'));
  2744. });
  2745. };
  2746. const onSetupTableHeaders = (command, headerType) => api => {
  2747. return onSetupWithToggle(api, targets => isCaption(targets.element), () => editor.queryCommandValue(command) === headerType);
  2748. };
  2749. const onSetupTableRowHeaders = onSetupTableHeaders('mceTableRowType', 'header');
  2750. const onSetupTableColumnHeaders = onSetupTableHeaders('mceTableColType', 'th');
  2751. editor.on('NodeChange ExecCommand TableSelectorChange', resetTargets);
  2752. return {
  2753. onSetupTable,
  2754. onSetupCellOrRow,
  2755. onSetupColumn,
  2756. onSetupPasteable,
  2757. onSetupPasteableColumn,
  2758. onSetupMergeable,
  2759. onSetupUnmergeable,
  2760. resetTargets,
  2761. onSetupTableWithCaption,
  2762. onSetupTableRowHeaders,
  2763. onSetupTableColumnHeaders,
  2764. targets: targets.get
  2765. };
  2766. };
  2767. var global = tinymce.util.Tools.resolve('tinymce.FakeClipboard');
  2768. const tableTypeBase = 'x-tinymce/dom-table-';
  2769. const tableTypeRow = tableTypeBase + 'rows';
  2770. const tableTypeColumn = tableTypeBase + 'columns';
  2771. const getData = type => {
  2772. var _a;
  2773. const items = (_a = global.read()) !== null && _a !== void 0 ? _a : [];
  2774. return findMap(items, item => Optional.from(item.getType(type)));
  2775. };
  2776. const getRows = () => getData(tableTypeRow);
  2777. const getColumns = () => getData(tableTypeColumn);
  2778. const addButtons = (editor, selectionTargets) => {
  2779. editor.ui.registry.addMenuButton('table', {
  2780. tooltip: 'Table',
  2781. icon: 'table',
  2782. fetch: callback => callback('inserttable | cell row column | advtablesort | tableprops deletetable')
  2783. });
  2784. const cmd = command => () => editor.execCommand(command);
  2785. const addButtonIfRegistered = (name, spec) => {
  2786. if (editor.queryCommandSupported(spec.command)) {
  2787. editor.ui.registry.addButton(name, {
  2788. ...spec,
  2789. onAction: isFunction(spec.onAction) ? spec.onAction : cmd(spec.command)
  2790. });
  2791. }
  2792. };
  2793. const addToggleButtonIfRegistered = (name, spec) => {
  2794. if (editor.queryCommandSupported(spec.command)) {
  2795. editor.ui.registry.addToggleButton(name, {
  2796. ...spec,
  2797. onAction: isFunction(spec.onAction) ? spec.onAction : cmd(spec.command)
  2798. });
  2799. }
  2800. };
  2801. addButtonIfRegistered('tableprops', {
  2802. tooltip: 'Table properties',
  2803. command: 'mceTableProps',
  2804. icon: 'table',
  2805. onSetup: selectionTargets.onSetupTable
  2806. });
  2807. addButtonIfRegistered('tabledelete', {
  2808. tooltip: 'Delete table',
  2809. command: 'mceTableDelete',
  2810. icon: 'table-delete-table',
  2811. onSetup: selectionTargets.onSetupTable
  2812. });
  2813. addButtonIfRegistered('tablecellprops', {
  2814. tooltip: 'Cell properties',
  2815. command: 'mceTableCellProps',
  2816. icon: 'table-cell-properties',
  2817. onSetup: selectionTargets.onSetupCellOrRow
  2818. });
  2819. addButtonIfRegistered('tablemergecells', {
  2820. tooltip: 'Merge cells',
  2821. command: 'mceTableMergeCells',
  2822. icon: 'table-merge-cells',
  2823. onSetup: selectionTargets.onSetupMergeable
  2824. });
  2825. addButtonIfRegistered('tablesplitcells', {
  2826. tooltip: 'Split cell',
  2827. command: 'mceTableSplitCells',
  2828. icon: 'table-split-cells',
  2829. onSetup: selectionTargets.onSetupUnmergeable
  2830. });
  2831. addButtonIfRegistered('tableinsertrowbefore', {
  2832. tooltip: 'Insert row before',
  2833. command: 'mceTableInsertRowBefore',
  2834. icon: 'table-insert-row-above',
  2835. onSetup: selectionTargets.onSetupCellOrRow
  2836. });
  2837. addButtonIfRegistered('tableinsertrowafter', {
  2838. tooltip: 'Insert row after',
  2839. command: 'mceTableInsertRowAfter',
  2840. icon: 'table-insert-row-after',
  2841. onSetup: selectionTargets.onSetupCellOrRow
  2842. });
  2843. addButtonIfRegistered('tabledeleterow', {
  2844. tooltip: 'Delete row',
  2845. command: 'mceTableDeleteRow',
  2846. icon: 'table-delete-row',
  2847. onSetup: selectionTargets.onSetupCellOrRow
  2848. });
  2849. addButtonIfRegistered('tablerowprops', {
  2850. tooltip: 'Row properties',
  2851. command: 'mceTableRowProps',
  2852. icon: 'table-row-properties',
  2853. onSetup: selectionTargets.onSetupCellOrRow
  2854. });
  2855. addButtonIfRegistered('tableinsertcolbefore', {
  2856. tooltip: 'Insert column before',
  2857. command: 'mceTableInsertColBefore',
  2858. icon: 'table-insert-column-before',
  2859. onSetup: selectionTargets.onSetupColumn('onFirst')
  2860. });
  2861. addButtonIfRegistered('tableinsertcolafter', {
  2862. tooltip: 'Insert column after',
  2863. command: 'mceTableInsertColAfter',
  2864. icon: 'table-insert-column-after',
  2865. onSetup: selectionTargets.onSetupColumn('onLast')
  2866. });
  2867. addButtonIfRegistered('tabledeletecol', {
  2868. tooltip: 'Delete column',
  2869. command: 'mceTableDeleteCol',
  2870. icon: 'table-delete-column',
  2871. onSetup: selectionTargets.onSetupColumn('onAny')
  2872. });
  2873. addButtonIfRegistered('tablecutrow', {
  2874. tooltip: 'Cut row',
  2875. command: 'mceTableCutRow',
  2876. icon: 'cut-row',
  2877. onSetup: selectionTargets.onSetupCellOrRow
  2878. });
  2879. addButtonIfRegistered('tablecopyrow', {
  2880. tooltip: 'Copy row',
  2881. command: 'mceTableCopyRow',
  2882. icon: 'duplicate-row',
  2883. onSetup: selectionTargets.onSetupCellOrRow
  2884. });
  2885. addButtonIfRegistered('tablepasterowbefore', {
  2886. tooltip: 'Paste row before',
  2887. command: 'mceTablePasteRowBefore',
  2888. icon: 'paste-row-before',
  2889. onSetup: selectionTargets.onSetupPasteable(getRows)
  2890. });
  2891. addButtonIfRegistered('tablepasterowafter', {
  2892. tooltip: 'Paste row after',
  2893. command: 'mceTablePasteRowAfter',
  2894. icon: 'paste-row-after',
  2895. onSetup: selectionTargets.onSetupPasteable(getRows)
  2896. });
  2897. addButtonIfRegistered('tablecutcol', {
  2898. tooltip: 'Cut column',
  2899. command: 'mceTableCutCol',
  2900. icon: 'cut-column',
  2901. onSetup: selectionTargets.onSetupColumn('onAny')
  2902. });
  2903. addButtonIfRegistered('tablecopycol', {
  2904. tooltip: 'Copy column',
  2905. command: 'mceTableCopyCol',
  2906. icon: 'duplicate-column',
  2907. onSetup: selectionTargets.onSetupColumn('onAny')
  2908. });
  2909. addButtonIfRegistered('tablepastecolbefore', {
  2910. tooltip: 'Paste column before',
  2911. command: 'mceTablePasteColBefore',
  2912. icon: 'paste-column-before',
  2913. onSetup: selectionTargets.onSetupPasteableColumn(getColumns, 'onFirst')
  2914. });
  2915. addButtonIfRegistered('tablepastecolafter', {
  2916. tooltip: 'Paste column after',
  2917. command: 'mceTablePasteColAfter',
  2918. icon: 'paste-column-after',
  2919. onSetup: selectionTargets.onSetupPasteableColumn(getColumns, 'onLast')
  2920. });
  2921. addButtonIfRegistered('tableinsertdialog', {
  2922. tooltip: 'Insert table',
  2923. command: 'mceInsertTableDialog',
  2924. icon: 'table'
  2925. });
  2926. const tableClassList = filterNoneItem(getTableClassList(editor));
  2927. if (tableClassList.length !== 0 && editor.queryCommandSupported('mceTableToggleClass')) {
  2928. editor.ui.registry.addMenuButton('tableclass', {
  2929. icon: 'table-classes',
  2930. tooltip: 'Table styles',
  2931. fetch: generateMenuItemsCallback(editor, tableClassList, 'tableclass', value => editor.execCommand('mceTableToggleClass', false, value)),
  2932. onSetup: selectionTargets.onSetupTable
  2933. });
  2934. }
  2935. const tableCellClassList = filterNoneItem(getCellClassList(editor));
  2936. if (tableCellClassList.length !== 0 && editor.queryCommandSupported('mceTableCellToggleClass')) {
  2937. editor.ui.registry.addMenuButton('tablecellclass', {
  2938. icon: 'table-cell-classes',
  2939. tooltip: 'Cell styles',
  2940. fetch: generateMenuItemsCallback(editor, tableCellClassList, 'tablecellclass', value => editor.execCommand('mceTableCellToggleClass', false, value)),
  2941. onSetup: selectionTargets.onSetupCellOrRow
  2942. });
  2943. }
  2944. if (editor.queryCommandSupported('mceTableApplyCellStyle')) {
  2945. editor.ui.registry.addMenuButton('tablecellvalign', {
  2946. icon: 'vertical-align',
  2947. tooltip: 'Vertical align',
  2948. fetch: generateMenuItemsCallback(editor, verticalAlignValues, 'tablecellverticalalign', applyTableCellStyle(editor, 'vertical-align')),
  2949. onSetup: selectionTargets.onSetupCellOrRow
  2950. });
  2951. editor.ui.registry.addMenuButton('tablecellborderwidth', {
  2952. icon: 'border-width',
  2953. tooltip: 'Border width',
  2954. fetch: generateMenuItemsCallback(editor, getTableBorderWidths(editor), 'tablecellborderwidth', applyTableCellStyle(editor, 'border-width')),
  2955. onSetup: selectionTargets.onSetupCellOrRow
  2956. });
  2957. editor.ui.registry.addMenuButton('tablecellborderstyle', {
  2958. icon: 'border-style',
  2959. tooltip: 'Border style',
  2960. fetch: generateMenuItemsCallback(editor, getTableBorderStyles(editor), 'tablecellborderstyle', applyTableCellStyle(editor, 'border-style')),
  2961. onSetup: selectionTargets.onSetupCellOrRow
  2962. });
  2963. editor.ui.registry.addMenuButton('tablecellbackgroundcolor', {
  2964. icon: 'cell-background-color',
  2965. tooltip: 'Background color',
  2966. fetch: callback => callback(buildColorMenu(editor, getTableBackgroundColorMap(editor), 'background-color')),
  2967. onSetup: selectionTargets.onSetupCellOrRow
  2968. });
  2969. editor.ui.registry.addMenuButton('tablecellbordercolor', {
  2970. icon: 'cell-border-color',
  2971. tooltip: 'Border color',
  2972. fetch: callback => callback(buildColorMenu(editor, getTableBorderColorMap(editor), 'border-color')),
  2973. onSetup: selectionTargets.onSetupCellOrRow
  2974. });
  2975. }
  2976. addToggleButtonIfRegistered('tablecaption', {
  2977. tooltip: 'Table caption',
  2978. icon: 'table-caption',
  2979. command: 'mceTableToggleCaption',
  2980. onSetup: selectionTargets.onSetupTableWithCaption
  2981. });
  2982. addToggleButtonIfRegistered('tablerowheader', {
  2983. tooltip: 'Row header',
  2984. icon: 'table-top-header',
  2985. command: 'mceTableRowType',
  2986. onAction: changeRowHeader(editor),
  2987. onSetup: selectionTargets.onSetupTableRowHeaders
  2988. });
  2989. addToggleButtonIfRegistered('tablecolheader', {
  2990. tooltip: 'Column header',
  2991. icon: 'table-left-header',
  2992. command: 'mceTableColType',
  2993. onAction: changeColumnHeader(editor),
  2994. onSetup: selectionTargets.onSetupTableColumnHeaders
  2995. });
  2996. };
  2997. const addToolbars = editor => {
  2998. const isTable = table => editor.dom.is(table, 'table') && editor.getBody().contains(table);
  2999. const toolbar = getToolbar(editor);
  3000. if (toolbar.length > 0) {
  3001. editor.ui.registry.addContextToolbar('table', {
  3002. predicate: isTable,
  3003. items: toolbar,
  3004. scope: 'node',
  3005. position: 'node'
  3006. });
  3007. }
  3008. };
  3009. const addMenuItems = (editor, selectionTargets) => {
  3010. const cmd = command => () => editor.execCommand(command);
  3011. const addMenuIfRegistered = (name, spec) => {
  3012. if (editor.queryCommandSupported(spec.command)) {
  3013. editor.ui.registry.addMenuItem(name, {
  3014. ...spec,
  3015. onAction: isFunction(spec.onAction) ? spec.onAction : cmd(spec.command)
  3016. });
  3017. return true;
  3018. } else {
  3019. return false;
  3020. }
  3021. };
  3022. const addToggleMenuIfRegistered = (name, spec) => {
  3023. if (editor.queryCommandSupported(spec.command)) {
  3024. editor.ui.registry.addToggleMenuItem(name, {
  3025. ...spec,
  3026. onAction: isFunction(spec.onAction) ? spec.onAction : cmd(spec.command)
  3027. });
  3028. }
  3029. };
  3030. const insertTableAction = data => {
  3031. editor.execCommand('mceInsertTable', false, {
  3032. rows: data.numRows,
  3033. columns: data.numColumns
  3034. });
  3035. };
  3036. const hasRowMenuItems = [
  3037. addMenuIfRegistered('tableinsertrowbefore', {
  3038. text: 'Insert row before',
  3039. icon: 'table-insert-row-above',
  3040. command: 'mceTableInsertRowBefore',
  3041. onSetup: selectionTargets.onSetupCellOrRow
  3042. }),
  3043. addMenuIfRegistered('tableinsertrowafter', {
  3044. text: 'Insert row after',
  3045. icon: 'table-insert-row-after',
  3046. command: 'mceTableInsertRowAfter',
  3047. onSetup: selectionTargets.onSetupCellOrRow
  3048. }),
  3049. addMenuIfRegistered('tabledeleterow', {
  3050. text: 'Delete row',
  3051. icon: 'table-delete-row',
  3052. command: 'mceTableDeleteRow',
  3053. onSetup: selectionTargets.onSetupCellOrRow
  3054. }),
  3055. addMenuIfRegistered('tablerowprops', {
  3056. text: 'Row properties',
  3057. icon: 'table-row-properties',
  3058. command: 'mceTableRowProps',
  3059. onSetup: selectionTargets.onSetupCellOrRow
  3060. }),
  3061. addMenuIfRegistered('tablecutrow', {
  3062. text: 'Cut row',
  3063. icon: 'cut-row',
  3064. command: 'mceTableCutRow',
  3065. onSetup: selectionTargets.onSetupCellOrRow
  3066. }),
  3067. addMenuIfRegistered('tablecopyrow', {
  3068. text: 'Copy row',
  3069. icon: 'duplicate-row',
  3070. command: 'mceTableCopyRow',
  3071. onSetup: selectionTargets.onSetupCellOrRow
  3072. }),
  3073. addMenuIfRegistered('tablepasterowbefore', {
  3074. text: 'Paste row before',
  3075. icon: 'paste-row-before',
  3076. command: 'mceTablePasteRowBefore',
  3077. onSetup: selectionTargets.onSetupPasteable(getRows)
  3078. }),
  3079. addMenuIfRegistered('tablepasterowafter', {
  3080. text: 'Paste row after',
  3081. icon: 'paste-row-after',
  3082. command: 'mceTablePasteRowAfter',
  3083. onSetup: selectionTargets.onSetupPasteable(getRows)
  3084. })
  3085. ];
  3086. const hasColumnMenuItems = [
  3087. addMenuIfRegistered('tableinsertcolumnbefore', {
  3088. text: 'Insert column before',
  3089. icon: 'table-insert-column-before',
  3090. command: 'mceTableInsertColBefore',
  3091. onSetup: selectionTargets.onSetupColumn('onFirst')
  3092. }),
  3093. addMenuIfRegistered('tableinsertcolumnafter', {
  3094. text: 'Insert column after',
  3095. icon: 'table-insert-column-after',
  3096. command: 'mceTableInsertColAfter',
  3097. onSetup: selectionTargets.onSetupColumn('onLast')
  3098. }),
  3099. addMenuIfRegistered('tabledeletecolumn', {
  3100. text: 'Delete column',
  3101. icon: 'table-delete-column',
  3102. command: 'mceTableDeleteCol',
  3103. onSetup: selectionTargets.onSetupColumn('onAny')
  3104. }),
  3105. addMenuIfRegistered('tablecutcolumn', {
  3106. text: 'Cut column',
  3107. icon: 'cut-column',
  3108. command: 'mceTableCutCol',
  3109. onSetup: selectionTargets.onSetupColumn('onAny')
  3110. }),
  3111. addMenuIfRegistered('tablecopycolumn', {
  3112. text: 'Copy column',
  3113. icon: 'duplicate-column',
  3114. command: 'mceTableCopyCol',
  3115. onSetup: selectionTargets.onSetupColumn('onAny')
  3116. }),
  3117. addMenuIfRegistered('tablepastecolumnbefore', {
  3118. text: 'Paste column before',
  3119. icon: 'paste-column-before',
  3120. command: 'mceTablePasteColBefore',
  3121. onSetup: selectionTargets.onSetupPasteableColumn(getColumns, 'onFirst')
  3122. }),
  3123. addMenuIfRegistered('tablepastecolumnafter', {
  3124. text: 'Paste column after',
  3125. icon: 'paste-column-after',
  3126. command: 'mceTablePasteColAfter',
  3127. onSetup: selectionTargets.onSetupPasteableColumn(getColumns, 'onLast')
  3128. })
  3129. ];
  3130. const hasCellMenuItems = [
  3131. addMenuIfRegistered('tablecellprops', {
  3132. text: 'Cell properties',
  3133. icon: 'table-cell-properties',
  3134. command: 'mceTableCellProps',
  3135. onSetup: selectionTargets.onSetupCellOrRow
  3136. }),
  3137. addMenuIfRegistered('tablemergecells', {
  3138. text: 'Merge cells',
  3139. icon: 'table-merge-cells',
  3140. command: 'mceTableMergeCells',
  3141. onSetup: selectionTargets.onSetupMergeable
  3142. }),
  3143. addMenuIfRegistered('tablesplitcells', {
  3144. text: 'Split cell',
  3145. icon: 'table-split-cells',
  3146. command: 'mceTableSplitCells',
  3147. onSetup: selectionTargets.onSetupUnmergeable
  3148. })
  3149. ];
  3150. if (!hasTableGrid(editor)) {
  3151. editor.ui.registry.addMenuItem('inserttable', {
  3152. text: 'Table',
  3153. icon: 'table',
  3154. onAction: cmd('mceInsertTableDialog')
  3155. });
  3156. } else {
  3157. editor.ui.registry.addNestedMenuItem('inserttable', {
  3158. text: 'Table',
  3159. icon: 'table',
  3160. getSubmenuItems: () => [{
  3161. type: 'fancymenuitem',
  3162. fancytype: 'inserttable',
  3163. onAction: insertTableAction
  3164. }]
  3165. });
  3166. }
  3167. editor.ui.registry.addMenuItem('inserttabledialog', {
  3168. text: 'Insert table',
  3169. icon: 'table',
  3170. onAction: cmd('mceInsertTableDialog')
  3171. });
  3172. addMenuIfRegistered('tableprops', {
  3173. text: 'Table properties',
  3174. onSetup: selectionTargets.onSetupTable,
  3175. command: 'mceTableProps'
  3176. });
  3177. addMenuIfRegistered('deletetable', {
  3178. text: 'Delete table',
  3179. icon: 'table-delete-table',
  3180. onSetup: selectionTargets.onSetupTable,
  3181. command: 'mceTableDelete'
  3182. });
  3183. if (contains(hasRowMenuItems, true)) {
  3184. editor.ui.registry.addNestedMenuItem('row', {
  3185. type: 'nestedmenuitem',
  3186. text: 'Row',
  3187. getSubmenuItems: constant('tableinsertrowbefore tableinsertrowafter tabledeleterow tablerowprops | tablecutrow tablecopyrow tablepasterowbefore tablepasterowafter')
  3188. });
  3189. }
  3190. if (contains(hasColumnMenuItems, true)) {
  3191. editor.ui.registry.addNestedMenuItem('column', {
  3192. type: 'nestedmenuitem',
  3193. text: 'Column',
  3194. getSubmenuItems: constant('tableinsertcolumnbefore tableinsertcolumnafter tabledeletecolumn | tablecutcolumn tablecopycolumn tablepastecolumnbefore tablepastecolumnafter')
  3195. });
  3196. }
  3197. if (contains(hasCellMenuItems, true)) {
  3198. editor.ui.registry.addNestedMenuItem('cell', {
  3199. type: 'nestedmenuitem',
  3200. text: 'Cell',
  3201. getSubmenuItems: constant('tablecellprops tablemergecells tablesplitcells')
  3202. });
  3203. }
  3204. editor.ui.registry.addContextMenu('table', {
  3205. update: () => {
  3206. selectionTargets.resetTargets();
  3207. return selectionTargets.targets().fold(constant(''), targets => {
  3208. if (name(targets.element) === 'caption') {
  3209. return 'tableprops deletetable';
  3210. } else {
  3211. return 'cell row column | advtablesort | tableprops deletetable';
  3212. }
  3213. });
  3214. }
  3215. });
  3216. const tableClassList = filterNoneItem(getTableClassList(editor));
  3217. if (tableClassList.length !== 0 && editor.queryCommandSupported('mceTableToggleClass')) {
  3218. editor.ui.registry.addNestedMenuItem('tableclass', {
  3219. icon: 'table-classes',
  3220. text: 'Table styles',
  3221. getSubmenuItems: () => buildMenuItems(editor, tableClassList, 'tableclass', value => editor.execCommand('mceTableToggleClass', false, value)),
  3222. onSetup: selectionTargets.onSetupTable
  3223. });
  3224. }
  3225. const tableCellClassList = filterNoneItem(getCellClassList(editor));
  3226. if (tableCellClassList.length !== 0 && editor.queryCommandSupported('mceTableCellToggleClass')) {
  3227. editor.ui.registry.addNestedMenuItem('tablecellclass', {
  3228. icon: 'table-cell-classes',
  3229. text: 'Cell styles',
  3230. getSubmenuItems: () => buildMenuItems(editor, tableCellClassList, 'tablecellclass', value => editor.execCommand('mceTableCellToggleClass', false, value)),
  3231. onSetup: selectionTargets.onSetupCellOrRow
  3232. });
  3233. }
  3234. if (editor.queryCommandSupported('mceTableApplyCellStyle')) {
  3235. editor.ui.registry.addNestedMenuItem('tablecellvalign', {
  3236. icon: 'vertical-align',
  3237. text: 'Vertical align',
  3238. getSubmenuItems: () => buildMenuItems(editor, verticalAlignValues, 'tablecellverticalalign', applyTableCellStyle(editor, 'vertical-align')),
  3239. onSetup: selectionTargets.onSetupCellOrRow
  3240. });
  3241. editor.ui.registry.addNestedMenuItem('tablecellborderwidth', {
  3242. icon: 'border-width',
  3243. text: 'Border width',
  3244. getSubmenuItems: () => buildMenuItems(editor, getTableBorderWidths(editor), 'tablecellborderwidth', applyTableCellStyle(editor, 'border-width')),
  3245. onSetup: selectionTargets.onSetupCellOrRow
  3246. });
  3247. editor.ui.registry.addNestedMenuItem('tablecellborderstyle', {
  3248. icon: 'border-style',
  3249. text: 'Border style',
  3250. getSubmenuItems: () => buildMenuItems(editor, getTableBorderStyles(editor), 'tablecellborderstyle', applyTableCellStyle(editor, 'border-style')),
  3251. onSetup: selectionTargets.onSetupCellOrRow
  3252. });
  3253. editor.ui.registry.addNestedMenuItem('tablecellbackgroundcolor', {
  3254. icon: 'cell-background-color',
  3255. text: 'Background color',
  3256. getSubmenuItems: () => buildColorMenu(editor, getTableBackgroundColorMap(editor), 'background-color'),
  3257. onSetup: selectionTargets.onSetupCellOrRow
  3258. });
  3259. editor.ui.registry.addNestedMenuItem('tablecellbordercolor', {
  3260. icon: 'cell-border-color',
  3261. text: 'Border color',
  3262. getSubmenuItems: () => buildColorMenu(editor, getTableBorderColorMap(editor), 'border-color'),
  3263. onSetup: selectionTargets.onSetupCellOrRow
  3264. });
  3265. }
  3266. addToggleMenuIfRegistered('tablecaption', {
  3267. icon: 'table-caption',
  3268. text: 'Table caption',
  3269. command: 'mceTableToggleCaption',
  3270. onSetup: selectionTargets.onSetupTableWithCaption
  3271. });
  3272. addToggleMenuIfRegistered('tablerowheader', {
  3273. text: 'Row header',
  3274. icon: 'table-top-header',
  3275. command: 'mceTableRowType',
  3276. onAction: changeRowHeader(editor),
  3277. onSetup: selectionTargets.onSetupTableRowHeaders
  3278. });
  3279. addToggleMenuIfRegistered('tablecolheader', {
  3280. text: 'Column header',
  3281. icon: 'table-left-header',
  3282. command: 'mceTableColType',
  3283. onAction: changeColumnHeader(editor),
  3284. onSetup: selectionTargets.onSetupTableRowHeaders
  3285. });
  3286. };
  3287. const Plugin = editor => {
  3288. const selectionTargets = getSelectionTargets(editor);
  3289. register(editor);
  3290. registerCommands(editor);
  3291. addMenuItems(editor, selectionTargets);
  3292. addButtons(editor, selectionTargets);
  3293. addToolbars(editor);
  3294. };
  3295. var Plugin$1 = () => {
  3296. global$3.add('table', Plugin);
  3297. };
  3298. Plugin$1();
  3299. })();