| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698 |
- /**
- * TinyMCE version 8.0.2 (2025-08-14)
- */
- (function () {
- 'use strict';
- var global$4 = tinymce.util.Tools.resolve('tinymce.PluginManager');
- /* eslint-disable @typescript-eslint/no-wrapper-object-types */
- const getPrototypeOf = Object.getPrototypeOf;
- const hasProto = (v, constructor, predicate) => {
- var _a;
- if (predicate(v, constructor.prototype)) {
- return true;
- }
- else {
- // String-based fallback time
- return ((_a = v.constructor) === null || _a === void 0 ? void 0 : _a.name) === constructor.name;
- }
- };
- const typeOf = (x) => {
- const t = typeof x;
- if (x === null) {
- return 'null';
- }
- else if (t === 'object' && Array.isArray(x)) {
- return 'array';
- }
- else if (t === 'object' && hasProto(x, String, (o, proto) => proto.isPrototypeOf(o))) {
- return 'string';
- }
- else {
- return t;
- }
- };
- const isType = (type) => (value) => typeOf(value) === type;
- const isSimpleType = (type) => (value) => typeof value === type;
- const eq = (t) => (a) => t === a;
- const is = (value, constructor) => isObject(value) && hasProto(value, constructor, (o, proto) => getPrototypeOf(o) === proto);
- const isString = isType('string');
- const isObject = isType('object');
- const isPlainObject = (value) => is(value, Object);
- const isArray = isType('array');
- const isNull = eq(null);
- const isBoolean = isSimpleType('boolean');
- const isNullable = (a) => a === null || a === undefined;
- const isNonNullable = (a) => !isNullable(a);
- const isFunction = isSimpleType('function');
- const isNumber = isSimpleType('number');
- const isArrayOf = (value, pred) => {
- if (isArray(value)) {
- for (let i = 0, len = value.length; i < len; ++i) {
- if (!(pred(value[i]))) {
- return false;
- }
- }
- return true;
- }
- return false;
- };
- const noop = () => { };
- /**
- * The `Optional` type represents a value (of any type) that potentially does
- * not exist. Any `Optional<T>` can either be a `Some<T>` (in which case the
- * value does exist) or a `None` (in which case the value does not exist). This
- * module defines a whole lot of FP-inspired utility functions for dealing with
- * `Optional` objects.
- *
- * Comparison with null or undefined:
- * - We don't get fancy null coalescing operators with `Optional`
- * - We do get fancy helper functions with `Optional`
- * - `Optional` support nesting, and allow for the type to still be nullable (or
- * another `Optional`)
- * - There is no option to turn off strict-optional-checks like there is for
- * strict-null-checks
- */
- class Optional {
- // The internal representation has a `tag` and a `value`, but both are
- // private: able to be console.logged, but not able to be accessed by code
- constructor(tag, value) {
- this.tag = tag;
- this.value = value;
- }
- // --- Identities ---
- /**
- * Creates a new `Optional<T>` that **does** contain a value.
- */
- static some(value) {
- return new Optional(true, value);
- }
- /**
- * Create a new `Optional<T>` that **does not** contain a value. `T` can be
- * any type because we don't actually have a `T`.
- */
- static none() {
- return Optional.singletonNone;
- }
- /**
- * Perform a transform on an `Optional` type. Regardless of whether this
- * `Optional` contains a value or not, `fold` will return a value of type `U`.
- * If this `Optional` does not contain a value, the `U` will be created by
- * calling `onNone`. If this `Optional` does contain a value, the `U` will be
- * created by calling `onSome`.
- *
- * For the FP enthusiasts in the room, this function:
- * 1. Could be used to implement all of the functions below
- * 2. Forms a catamorphism
- */
- fold(onNone, onSome) {
- if (this.tag) {
- return onSome(this.value);
- }
- else {
- return onNone();
- }
- }
- /**
- * Determine if this `Optional` object contains a value.
- */
- isSome() {
- return this.tag;
- }
- /**
- * Determine if this `Optional` object **does not** contain a value.
- */
- isNone() {
- return !this.tag;
- }
- // --- Functor (name stolen from Haskell / maths) ---
- /**
- * Perform a transform on an `Optional` object, **if** there is a value. If
- * you provide a function to turn a T into a U, this is the function you use
- * to turn an `Optional<T>` into an `Optional<U>`. If this **does** contain
- * a value then the output will also contain a value (that value being the
- * output of `mapper(this.value)`), and if this **does not** contain a value
- * then neither will the output.
- */
- map(mapper) {
- if (this.tag) {
- return Optional.some(mapper(this.value));
- }
- else {
- return Optional.none();
- }
- }
- // --- Monad (name stolen from Haskell / maths) ---
- /**
- * Perform a transform on an `Optional` object, **if** there is a value.
- * Unlike `map`, here the transform itself also returns an `Optional`.
- */
- bind(binder) {
- if (this.tag) {
- return binder(this.value);
- }
- else {
- return Optional.none();
- }
- }
- // --- Traversable (name stolen from Haskell / maths) ---
- /**
- * For a given predicate, this function finds out if there **exists** a value
- * inside this `Optional` object that meets the predicate. In practice, this
- * means that for `Optional`s that do not contain a value it returns false (as
- * no predicate-meeting value exists).
- */
- exists(predicate) {
- return this.tag && predicate(this.value);
- }
- /**
- * For a given predicate, this function finds out if **all** the values inside
- * this `Optional` object meet the predicate. In practice, this means that
- * for `Optional`s that do not contain a value it returns true (as all 0
- * objects do meet the predicate).
- */
- forall(predicate) {
- return !this.tag || predicate(this.value);
- }
- filter(predicate) {
- if (!this.tag || predicate(this.value)) {
- return this;
- }
- else {
- return Optional.none();
- }
- }
- // --- Getters ---
- /**
- * Get the value out of the inside of the `Optional` object, using a default
- * `replacement` value if the provided `Optional` object does not contain a
- * value.
- */
- getOr(replacement) {
- return this.tag ? this.value : replacement;
- }
- /**
- * Get the value out of the inside of the `Optional` object, using a default
- * `replacement` value if the provided `Optional` object does not contain a
- * value. Unlike `getOr`, in this method the `replacement` object is also
- * `Optional` - meaning that this method will always return an `Optional`.
- */
- or(replacement) {
- return this.tag ? this : replacement;
- }
- /**
- * Get the value out of the inside of the `Optional` object, using a default
- * `replacement` value if the provided `Optional` object does not contain a
- * value. Unlike `getOr`, in this method the `replacement` value is
- * "thunked" - that is to say that you don't pass a value to `getOrThunk`, you
- * pass a function which (if called) will **return** the `value` you want to
- * use.
- */
- getOrThunk(thunk) {
- return this.tag ? this.value : thunk();
- }
- /**
- * Get the value out of the inside of the `Optional` object, using a default
- * `replacement` value if the provided Optional object does not contain a
- * value.
- *
- * Unlike `or`, in this method the `replacement` value is "thunked" - that is
- * to say that you don't pass a value to `orThunk`, you pass a function which
- * (if called) will **return** the `value` you want to use.
- *
- * Unlike `getOrThunk`, in this method the `replacement` value is also
- * `Optional`, meaning that this method will always return an `Optional`.
- */
- orThunk(thunk) {
- return this.tag ? this : thunk();
- }
- /**
- * Get the value out of the inside of the `Optional` object, throwing an
- * exception if the provided `Optional` object does not contain a value.
- *
- * WARNING:
- * You should only be using this function if you know that the `Optional`
- * object **is not** empty (otherwise you're throwing exceptions in production
- * code, which is bad).
- *
- * In tests this is more acceptable.
- *
- * Prefer other methods to this, such as `.each`.
- */
- getOrDie(message) {
- if (!this.tag) {
- throw new Error(message !== null && message !== void 0 ? message : 'Called getOrDie on None');
- }
- else {
- return this.value;
- }
- }
- // --- Interop with null and undefined ---
- /**
- * Creates an `Optional` value from a nullable (or undefined-able) input.
- * Null, or undefined, is converted to `None`, and anything else is converted
- * to `Some`.
- */
- static from(value) {
- return isNonNullable(value) ? Optional.some(value) : Optional.none();
- }
- /**
- * Converts an `Optional` to a nullable type, by getting the value if it
- * exists, or returning `null` if it does not.
- */
- getOrNull() {
- return this.tag ? this.value : null;
- }
- /**
- * Converts an `Optional` to an undefined-able type, by getting the value if
- * it exists, or returning `undefined` if it does not.
- */
- getOrUndefined() {
- return this.value;
- }
- // --- Utilities ---
- /**
- * If the `Optional` contains a value, perform an action on that value.
- * Unlike the rest of the methods on this type, `.each` has side-effects. If
- * you want to transform an `Optional<T>` **into** something, then this is not
- * the method for you. If you want to use an `Optional<T>` to **do**
- * something, then this is the method for you - provided you're okay with not
- * doing anything in the case where the `Optional` doesn't have a value inside
- * it. If you're not sure whether your use-case fits into transforming
- * **into** something or **doing** something, check whether it has a return
- * value. If it does, you should be performing a transform.
- */
- each(worker) {
- if (this.tag) {
- worker(this.value);
- }
- }
- /**
- * Turn the `Optional` object into an array that contains all of the values
- * stored inside the `Optional`. In practice, this means the output will have
- * either 0 or 1 elements.
- */
- toArray() {
- return this.tag ? [this.value] : [];
- }
- /**
- * Turn the `Optional` object into a string for debugging or printing. Not
- * recommended for production code, but good for debugging. Also note that
- * these days an `Optional` object can be logged to the console directly, and
- * its inner value (if it exists) will be visible.
- */
- toString() {
- return this.tag ? `some(${this.value})` : 'none()';
- }
- }
- // Sneaky optimisation: every instance of Optional.none is identical, so just
- // reuse the same object
- Optional.singletonNone = new Optional(false);
- const nativeSlice = Array.prototype.slice;
- const nativePush = Array.prototype.push;
- const flatten = (xs) => {
- // Note, this is possible because push supports multiple arguments:
- // http://jsperf.com/concat-push/6
- // Note that in the past, concat() would silently work (very slowly) for array-like objects.
- // With this change it will throw an error.
- const r = [];
- for (let i = 0, len = xs.length; i < len; ++i) {
- // Ensure that each value is an array itself
- if (!isArray(xs[i])) {
- throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
- }
- nativePush.apply(r, xs[i]);
- }
- return r;
- };
- const get = (xs, i) => i >= 0 && i < xs.length ? Optional.some(xs[i]) : Optional.none();
- const head = (xs) => get(xs, 0);
- isFunction(Array.from) ? Array.from : (x) => nativeSlice.call(x);
- const findMap = (arr, f) => {
- for (let i = 0; i < arr.length; i++) {
- const r = f(arr[i], i);
- if (r.isSome()) {
- return r;
- }
- }
- return Optional.none();
- };
- // There are many variations of Object iteration that are faster than the 'for-in' style:
- // http://jsperf.com/object-keys-iteration/107
- //
- // Use the native keys if it is available (IE9+), otherwise fall back to manually filtering
- const keys = Object.keys;
- const hasOwnProperty = Object.hasOwnProperty;
- const each = (obj, f) => {
- const props = keys(obj);
- for (let k = 0, len = props.length; k < len; k++) {
- const i = props[k];
- const x = obj[i];
- f(x, i);
- }
- };
- const objAcc = (r) => (x, i) => {
- r[i] = x;
- };
- const internalFilter = (obj, pred, onTrue, onFalse) => {
- each(obj, (x, i) => {
- (pred(x, i) ? onTrue : onFalse)(x, i);
- });
- };
- const filter = (obj, pred) => {
- const t = {};
- internalFilter(obj, pred, objAcc(t), noop);
- return t;
- };
- const has = (obj, key) => hasOwnProperty.call(obj, key);
- const hasNonNullableKey = (obj, key) => has(obj, key) && obj[key] !== undefined && obj[key] !== null;
- const deep = (old, nu) => {
- const bothObjects = isPlainObject(old) && isPlainObject(nu);
- return bothObjects ? deepMerge(old, nu) : nu;
- };
- const baseMerge = (merger) => {
- return (...objects) => {
- if (objects.length === 0) {
- throw new Error(`Can't merge zero objects`);
- }
- const ret = {};
- for (let j = 0; j < objects.length; j++) {
- const curObject = objects[j];
- for (const key in curObject) {
- if (has(curObject, key)) {
- ret[key] = merger(ret[key], curObject[key]);
- }
- }
- }
- return ret;
- };
- };
- const deepMerge = baseMerge(deep);
- const isNotEmpty = (s) => s.length > 0;
- const fromHtml = (html, scope) => {
- const doc = scope || document;
- const div = doc.createElement('div');
- div.innerHTML = html;
- if (!div.hasChildNodes() || div.childNodes.length > 1) {
- const message = 'HTML does not have a single root node';
- // eslint-disable-next-line no-console
- console.error(message, html);
- throw new Error(message);
- }
- return fromDom(div.childNodes[0]);
- };
- const fromTag = (tag, scope) => {
- const doc = scope || document;
- const node = doc.createElement(tag);
- return fromDom(node);
- };
- const fromText = (text, scope) => {
- const doc = scope || document;
- const node = doc.createTextNode(text);
- return fromDom(node);
- };
- const fromDom = (node) => {
- // TODO: Consider removing this check, but left atm for safety
- if (node === null || node === undefined) {
- throw new Error('Node cannot be null or undefined');
- }
- return {
- dom: node
- };
- };
- const fromPoint = (docElm, x, y) => Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
- // tslint:disable-next-line:variable-name
- const SugarElement = {
- fromHtml,
- fromTag,
- fromText,
- fromDom,
- fromPoint
- };
- const rawSet = (dom, key, value) => {
- /*
- * JQuery coerced everything to a string, and silently did nothing on text node/null/undefined.
- *
- * We fail on those invalid cases, only allowing numbers and booleans.
- */
- if (isString(value) || isBoolean(value) || isNumber(value)) {
- dom.setAttribute(key, value + '');
- }
- else {
- // eslint-disable-next-line no-console
- console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
- throw new Error('Attribute value was not simple');
- }
- };
- const set = (element, key, value) => {
- rawSet(element.dom, key, value);
- };
- const remove = (element, key) => {
- element.dom.removeAttribute(key);
- };
- var global$3 = tinymce.util.Tools.resolve('tinymce.dom.DOMUtils');
- var global$2 = tinymce.util.Tools.resolve('tinymce.util.URI');
- const option = (name) => (editor) => editor.options.get(name);
- const register$2 = (editor) => {
- const registerOption = editor.options.register;
- registerOption('image_dimensions', {
- processor: 'boolean',
- default: true
- });
- registerOption('image_advtab', {
- processor: 'boolean',
- default: false
- });
- registerOption('image_uploadtab', {
- processor: 'boolean',
- default: true
- });
- registerOption('image_prepend_url', {
- processor: 'string',
- default: ''
- });
- registerOption('image_class_list', {
- processor: 'object[]'
- });
- registerOption('image_description', {
- processor: 'boolean',
- default: true
- });
- registerOption('image_title', {
- processor: 'boolean',
- default: false
- });
- registerOption('image_caption', {
- processor: 'boolean',
- default: false
- });
- registerOption('image_list', {
- processor: (value) => {
- const valid = value === false || isString(value) || isArrayOf(value, isObject) || isFunction(value);
- return valid ? { value, valid } : { valid: false, message: 'Must be false, a string, an array or a function.' };
- },
- default: false
- });
- };
- const hasDimensions = option('image_dimensions');
- const hasAdvTab = option('image_advtab');
- const hasUploadTab = option('image_uploadtab');
- const getPrependUrl = option('image_prepend_url');
- const getClassList = option('image_class_list');
- const hasDescription = option('image_description');
- const hasImageTitle = option('image_title');
- const hasImageCaption = option('image_caption');
- const getImageList = option('image_list');
- const showAccessibilityOptions = option('a11y_advanced_options');
- const isAutomaticUploadsEnabled = option('automatic_uploads');
- const hasUploadUrl = (editor) => isNotEmpty(editor.options.get('images_upload_url'));
- const hasUploadHandler = (editor) => isNonNullable(editor.options.get('images_upload_handler'));
- // TODO: Figure out if these would ever be something other than numbers. This was added in: #TINY-1350
- const parseIntAndGetMax = (val1, val2) => Math.max(parseInt(val1, 10), parseInt(val2, 10));
- const getImageSize = (url) => new Promise((callback) => {
- const img = document.createElement('img');
- const done = (dimensions) => {
- if (img.parentNode) {
- img.parentNode.removeChild(img);
- }
- callback(dimensions);
- };
- img.addEventListener('load', () => {
- const width = parseIntAndGetMax(img.width, img.clientWidth);
- const height = parseIntAndGetMax(img.height, img.clientHeight);
- const dimensions = { width, height };
- done(Promise.resolve(dimensions));
- });
- img.addEventListener('error', () => {
- done(Promise.reject(`Failed to get image dimensions for: ${url}`));
- });
- const style = img.style;
- style.visibility = 'hidden';
- style.position = 'fixed';
- style.bottom = style.left = '0px';
- style.width = style.height = 'auto';
- document.body.appendChild(img);
- img.src = url;
- });
- const removePixelSuffix = (value) => {
- if (value) {
- value = value.replace(/px$/, '');
- }
- return value;
- };
- const addPixelSuffix = (value) => {
- if (value.length > 0 && /^[0-9]+$/.test(value)) {
- value += 'px';
- }
- return value;
- };
- const mergeMargins = (css) => {
- if (css.margin) {
- const splitMargin = String(css.margin).split(' ');
- switch (splitMargin.length) {
- case 1: // margin: toprightbottomleft;
- css['margin-top'] = css['margin-top'] || splitMargin[0];
- css['margin-right'] = css['margin-right'] || splitMargin[0];
- css['margin-bottom'] = css['margin-bottom'] || splitMargin[0];
- css['margin-left'] = css['margin-left'] || splitMargin[0];
- break;
- case 2: // margin: topbottom rightleft;
- css['margin-top'] = css['margin-top'] || splitMargin[0];
- css['margin-right'] = css['margin-right'] || splitMargin[1];
- css['margin-bottom'] = css['margin-bottom'] || splitMargin[0];
- css['margin-left'] = css['margin-left'] || splitMargin[1];
- break;
- case 3: // margin: top rightleft bottom;
- css['margin-top'] = css['margin-top'] || splitMargin[0];
- css['margin-right'] = css['margin-right'] || splitMargin[1];
- css['margin-bottom'] = css['margin-bottom'] || splitMargin[2];
- css['margin-left'] = css['margin-left'] || splitMargin[1];
- break;
- case 4: // margin: top right bottom left;
- css['margin-top'] = css['margin-top'] || splitMargin[0];
- css['margin-right'] = css['margin-right'] || splitMargin[1];
- css['margin-bottom'] = css['margin-bottom'] || splitMargin[2];
- css['margin-left'] = css['margin-left'] || splitMargin[3];
- }
- delete css.margin;
- }
- return css;
- };
- // TODO: Input on this callback should really be validated
- const createImageList = (editor, callback) => {
- const imageList = getImageList(editor);
- if (isString(imageList)) {
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- fetch(imageList)
- .then((res) => {
- if (res.ok) {
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- res.json().then(callback);
- }
- });
- }
- else if (isFunction(imageList)) {
- imageList(callback);
- }
- else {
- callback(imageList);
- }
- };
- const waitLoadImage = (editor, data, imgElm) => {
- const selectImage = () => {
- imgElm.onload = imgElm.onerror = null;
- if (editor.selection) {
- editor.selection.select(imgElm);
- editor.nodeChanged();
- }
- };
- imgElm.onload = () => {
- if (!data.width && !data.height && hasDimensions(editor)) {
- editor.dom.setAttribs(imgElm, {
- width: String(imgElm.clientWidth),
- height: String(imgElm.clientHeight)
- });
- }
- selectImage();
- };
- imgElm.onerror = selectImage;
- };
- const blobToDataUri = (blob) => new Promise((resolve, reject) => {
- const reader = new FileReader();
- reader.onload = () => {
- resolve(reader.result);
- };
- reader.onerror = () => {
- var _a;
- reject((_a = reader.error) === null || _a === void 0 ? void 0 : _a.message);
- };
- reader.readAsDataURL(blob);
- });
- const isPlaceholderImage = (imgElm) => imgElm.nodeName === 'IMG' && (imgElm.hasAttribute('data-mce-object') || imgElm.hasAttribute('data-mce-placeholder'));
- const isSafeImageUrl = (editor, src) => {
- const getOption = editor.options.get;
- return global$2.isDomSafe(src, 'img', {
- allow_html_data_urls: getOption('allow_html_data_urls'),
- allow_script_urls: getOption('allow_script_urls'),
- allow_svg_data_urls: getOption('allow_svg_data_urls')
- });
- };
- const DOM = global$3.DOM;
- const getHspace = (image) => {
- if (image.style.marginLeft && image.style.marginRight && image.style.marginLeft === image.style.marginRight) {
- return removePixelSuffix(image.style.marginLeft);
- }
- else {
- return '';
- }
- };
- const getVspace = (image) => {
- if (image.style.marginTop && image.style.marginBottom && image.style.marginTop === image.style.marginBottom) {
- return removePixelSuffix(image.style.marginTop);
- }
- else {
- return '';
- }
- };
- const getBorder = (image) => {
- if (image.style.borderWidth) {
- return removePixelSuffix(image.style.borderWidth);
- }
- else {
- return '';
- }
- };
- const getAttrib = (image, name) => {
- var _a;
- if (image.hasAttribute(name)) {
- return (_a = image.getAttribute(name)) !== null && _a !== void 0 ? _a : '';
- }
- else {
- return '';
- }
- };
- const hasCaption = (image) => image.parentNode !== null && image.parentNode.nodeName === 'FIGURE';
- const updateAttrib = (image, name, value) => {
- if (value === '' || value === null) {
- image.removeAttribute(name);
- }
- else {
- image.setAttribute(name, value);
- }
- };
- const wrapInFigure = (image) => {
- const figureElm = DOM.create('figure', { class: 'image' });
- DOM.insertAfter(figureElm, image);
- figureElm.appendChild(image);
- figureElm.appendChild(DOM.create('figcaption', { contentEditable: 'true' }, 'Caption'));
- figureElm.contentEditable = 'false';
- };
- const removeFigure = (image) => {
- const figureElm = image.parentNode;
- if (isNonNullable(figureElm)) {
- DOM.insertAfter(image, figureElm);
- DOM.remove(figureElm);
- }
- };
- const toggleCaption = (image) => {
- if (hasCaption(image)) {
- removeFigure(image);
- }
- else {
- wrapInFigure(image);
- }
- };
- const normalizeStyle = (image, normalizeCss) => {
- const attrValue = image.getAttribute('style');
- const value = normalizeCss(attrValue !== null ? attrValue : '');
- if (value.length > 0) {
- image.setAttribute('style', value);
- image.setAttribute('data-mce-style', value);
- }
- else {
- image.removeAttribute('style');
- }
- };
- const setSize = (name, normalizeCss) => (image, name, value) => {
- const styles = image.style;
- if (styles[name]) {
- styles[name] = addPixelSuffix(value);
- normalizeStyle(image, normalizeCss);
- }
- else {
- updateAttrib(image, name, value);
- }
- };
- const getSize = (image, name) => {
- if (image.style[name]) {
- return removePixelSuffix(image.style[name]);
- }
- else {
- return getAttrib(image, name);
- }
- };
- const setHspace = (image, value) => {
- const pxValue = addPixelSuffix(value);
- image.style.marginLeft = pxValue;
- image.style.marginRight = pxValue;
- };
- const setVspace = (image, value) => {
- const pxValue = addPixelSuffix(value);
- image.style.marginTop = pxValue;
- image.style.marginBottom = pxValue;
- };
- const setBorder = (image, value) => {
- const pxValue = addPixelSuffix(value);
- image.style.borderWidth = pxValue;
- };
- const setBorderStyle = (image, value) => {
- image.style.borderStyle = value;
- };
- const getBorderStyle = (image) => { var _a; return (_a = image.style.borderStyle) !== null && _a !== void 0 ? _a : ''; };
- const isFigure = (elm) => isNonNullable(elm) && elm.nodeName === 'FIGURE';
- const isImage = (elm) => elm.nodeName === 'IMG';
- const getIsDecorative = (image) => {
- const alt = DOM.getAttrib(image, 'alt');
- const role = DOM.getAttrib(image, 'role');
- // WCAG Technique H67: Using null alt text and no title attribute on img elements for images that AT should ignore
- // Source: https://www.w3.org/TR/WCAG20-TECHS/H67.html
- // Key point: Decorative images should have alt="" and either no title or empty title (title="")
- // ARIA 1.2 Specification: Defines role="presentation" and role="none" as synonymous roles
- // Source: https://www.w3.org/TR/wai-aria-1.2/
- // Key point: These roles remove semantic meaning and prohibit aria-label and aria-labelledby
- const hasAlt = image.hasAttribute('alt');
- return (hasAlt && alt.length === 0) || (role === 'presentation') || (role === 'none');
- };
- const getAlt = (image) => {
- if (getIsDecorative(image)) {
- return '';
- }
- else {
- return getAttrib(image, 'alt');
- }
- };
- const defaultData = () => ({
- src: '',
- alt: '',
- title: '',
- width: '',
- height: '',
- class: '',
- style: '',
- caption: false,
- hspace: '',
- vspace: '',
- border: '',
- borderStyle: '',
- isDecorative: false
- });
- const getStyleValue = (normalizeCss, data) => {
- var _a;
- const image = document.createElement('img');
- updateAttrib(image, 'style', data.style);
- if (getHspace(image) || data.hspace !== '') {
- setHspace(image, data.hspace);
- }
- if (getVspace(image) || data.vspace !== '') {
- setVspace(image, data.vspace);
- }
- if (getBorder(image) || data.border !== '') {
- setBorder(image, data.border);
- }
- if (getBorderStyle(image) || data.borderStyle !== '') {
- setBorderStyle(image, data.borderStyle);
- }
- return normalizeCss((_a = image.getAttribute('style')) !== null && _a !== void 0 ? _a : '');
- };
- const create = (normalizeCss, data) => {
- const image = document.createElement('img');
- write(normalizeCss, { ...data, caption: false }, image);
- // Always set alt even if data.alt is an empty string
- setAlt(image, data.alt, data.isDecorative);
- if (data.caption) {
- const figure = DOM.create('figure', { class: 'image' });
- figure.appendChild(image);
- figure.appendChild(DOM.create('figcaption', { contentEditable: 'true' }, 'Caption'));
- figure.contentEditable = 'false';
- return figure;
- }
- else {
- return image;
- }
- };
- const read = (normalizeCss, image) => ({
- src: getAttrib(image, 'src'),
- alt: getAlt(image),
- title: getAttrib(image, 'title'),
- width: getSize(image, 'width'),
- height: getSize(image, 'height'),
- class: getAttrib(image, 'class'),
- style: normalizeCss(getAttrib(image, 'style')),
- caption: hasCaption(image),
- hspace: getHspace(image),
- vspace: getVspace(image),
- border: getBorder(image),
- borderStyle: getBorderStyle(image),
- isDecorative: getIsDecorative(image)
- });
- const updateProp = (image, oldData, newData, name, set) => {
- if (newData[name] !== oldData[name]) {
- set(image, name, String(newData[name]));
- }
- };
- const setAlt = (image, alt, isDecorative) => {
- if (isDecorative) {
- DOM.setAttrib(image, 'role', 'presentation');
- // unfortunately can't set "" attr value with domutils
- const sugarImage = SugarElement.fromDom(image);
- set(sugarImage, 'alt', '');
- }
- else {
- if (isNull(alt)) {
- const sugarImage = SugarElement.fromDom(image);
- remove(sugarImage, 'alt');
- }
- else {
- // unfortunately can't set "" attr value with domutils
- const sugarImage = SugarElement.fromDom(image);
- set(sugarImage, 'alt', alt);
- }
- if (DOM.getAttrib(image, 'role') === 'presentation') {
- DOM.setAttrib(image, 'role', '');
- }
- }
- };
- const updateAlt = (image, oldData, newData) => {
- if (newData.alt !== oldData.alt || newData.isDecorative !== oldData.isDecorative) {
- setAlt(image, newData.alt, newData.isDecorative);
- }
- };
- const normalized = (set, normalizeCss) => (image, name, value) => {
- set(image, value);
- normalizeStyle(image, normalizeCss);
- };
- const write = (normalizeCss, newData, image) => {
- const oldData = read(normalizeCss, image);
- updateProp(image, oldData, newData, 'caption', (image, _name, _value) => toggleCaption(image));
- updateProp(image, oldData, newData, 'src', updateAttrib);
- updateProp(image, oldData, newData, 'title', updateAttrib);
- updateProp(image, oldData, newData, 'width', setSize('width', normalizeCss));
- updateProp(image, oldData, newData, 'height', setSize('height', normalizeCss));
- updateProp(image, oldData, newData, 'class', updateAttrib);
- updateProp(image, oldData, newData, 'style', normalized((image, value) => updateAttrib(image, 'style', value), normalizeCss));
- updateProp(image, oldData, newData, 'hspace', normalized(setHspace, normalizeCss));
- updateProp(image, oldData, newData, 'vspace', normalized(setVspace, normalizeCss));
- updateProp(image, oldData, newData, 'border', normalized(setBorder, normalizeCss));
- updateProp(image, oldData, newData, 'borderStyle', normalized(setBorderStyle, normalizeCss));
- updateAlt(image, oldData, newData);
- };
- const normalizeCss$1 = (editor, cssText) => {
- const css = editor.dom.styles.parse(cssText);
- const mergedCss = mergeMargins(css);
- const compressed = editor.dom.styles.parse(editor.dom.styles.serialize(mergedCss));
- return editor.dom.styles.serialize(compressed);
- };
- const getSelectedImage = (editor) => {
- const imgElm = editor.selection.getNode();
- const figureElm = editor.dom.getParent(imgElm, 'figure.image');
- if (figureElm) {
- return editor.dom.select('img', figureElm)[0];
- }
- if (imgElm && (imgElm.nodeName !== 'IMG' || isPlaceholderImage(imgElm))) {
- return null;
- }
- return imgElm;
- };
- const splitTextBlock = (editor, figure) => {
- var _a;
- const dom = editor.dom;
- const textBlockElements = filter(editor.schema.getTextBlockElements(), (_, parentElm) => !editor.schema.isValidChild(parentElm, 'figure'));
- const textBlock = dom.getParent(figure.parentNode, (node) => hasNonNullableKey(textBlockElements, node.nodeName), editor.getBody());
- if (textBlock) {
- return (_a = dom.split(textBlock, figure)) !== null && _a !== void 0 ? _a : figure;
- }
- else {
- return figure;
- }
- };
- const readImageDataFromSelection = (editor) => {
- const image = getSelectedImage(editor);
- return image ? read((css) => normalizeCss$1(editor, css), image) : defaultData();
- };
- const insertImageAtCaret = (editor, data) => {
- const elm = create((css) => normalizeCss$1(editor, css), data);
- editor.dom.setAttrib(elm, 'data-mce-id', '__mcenew');
- editor.focus();
- editor.insertContent(elm.outerHTML);
- const insertedElm = editor.dom.select('*[data-mce-id="__mcenew"]')[0];
- editor.dom.setAttrib(insertedElm, 'data-mce-id', null);
- if (isFigure(insertedElm)) {
- const figure = splitTextBlock(editor, insertedElm);
- editor.selection.select(figure);
- }
- else {
- editor.selection.select(insertedElm);
- }
- };
- const syncSrcAttr = (editor, image) => {
- editor.dom.setAttrib(image, 'src', image.getAttribute('src'));
- };
- const deleteImage = (editor, image) => {
- if (image) {
- const elm = editor.dom.is(image.parentNode, 'figure.image') ? image.parentNode : image;
- editor.dom.remove(elm);
- editor.focus();
- editor.nodeChanged();
- if (editor.dom.isEmpty(editor.getBody())) {
- editor.setContent('');
- editor.selection.setCursorLocation();
- }
- }
- };
- const writeImageDataToSelection = (editor, data) => {
- const image = getSelectedImage(editor);
- if (image) {
- write((css) => normalizeCss$1(editor, css), data, image);
- syncSrcAttr(editor, image);
- if (isFigure(image.parentNode)) {
- editor.dom.setStyle(image, 'float', '');
- const figure = image.parentNode;
- splitTextBlock(editor, figure);
- editor.selection.select(image.parentNode);
- }
- else {
- editor.selection.select(image);
- waitLoadImage(editor, data, image);
- }
- }
- };
- const sanitizeImageData = (editor, data) => {
- // Sanitize the URL
- const src = data.src;
- return {
- ...data,
- src: isSafeImageUrl(editor, src) ? src : ''
- };
- };
- const insertOrUpdateImage = (editor, partialData) => {
- const image = getSelectedImage(editor);
- if (image) {
- const selectedImageData = read((css) => normalizeCss$1(editor, css), image);
- const data = { ...selectedImageData, ...partialData };
- const sanitizedData = sanitizeImageData(editor, data);
- if (data.src) {
- writeImageDataToSelection(editor, sanitizedData);
- }
- else {
- deleteImage(editor, image);
- }
- }
- else if (partialData.src) {
- insertImageAtCaret(editor, { ...defaultData(), ...partialData });
- }
- };
- var global$1 = tinymce.util.Tools.resolve('tinymce.util.ImageUploader');
- var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
- const getValue = (item) => isString(item.value) ? item.value : '';
- const getText = (item) => {
- if (isString(item.text)) {
- return item.text;
- }
- else if (isString(item.title)) {
- return item.title;
- }
- else {
- return '';
- }
- };
- const sanitizeList = (list, extractValue) => {
- const out = [];
- global.each(list, (item) => {
- const text = getText(item);
- if (item.menu !== undefined) {
- const items = sanitizeList(item.menu, extractValue);
- out.push({ text, items }); // list group
- }
- else {
- const value = extractValue(item);
- out.push({ text, value }); // list value
- }
- });
- return out;
- };
- const sanitizer = (extractor = getValue) => (list) => {
- if (list) {
- return Optional.from(list).map((list) => sanitizeList(list, extractor));
- }
- else {
- return Optional.none();
- }
- };
- const sanitize = (list) => sanitizer(getValue)(list);
- const isGroup = (item) => has(item, 'items');
- const findEntryDelegate = (list, value) => findMap(list, (item) => {
- if (isGroup(item)) {
- return findEntryDelegate(item.items, value);
- }
- else if (item.value === value) {
- return Optional.some(item);
- }
- else {
- return Optional.none();
- }
- });
- const findEntry = (optList, value) => optList.bind((list) => findEntryDelegate(list, value));
- const ListUtils = {
- sanitizer,
- sanitize,
- findEntry
- };
- const makeTab$2 = (_info) => ({
- title: 'Advanced',
- name: 'advanced',
- items: [
- {
- type: 'grid',
- columns: 2,
- items: [
- {
- type: 'input',
- label: 'Vertical space',
- name: 'vspace',
- inputMode: 'numeric'
- },
- {
- type: 'input',
- label: 'Horizontal space',
- name: 'hspace',
- inputMode: 'numeric'
- },
- {
- type: 'input',
- label: 'Border width',
- name: 'border',
- inputMode: 'numeric'
- },
- {
- type: 'listbox',
- name: 'borderstyle',
- label: 'Border style',
- items: [
- { text: 'Select...', value: '' },
- { text: 'Solid', value: 'solid' },
- { text: 'Dotted', value: 'dotted' },
- { text: 'Dashed', value: 'dashed' },
- { text: 'Double', value: 'double' },
- { text: 'Groove', value: 'groove' },
- { text: 'Ridge', value: 'ridge' },
- { text: 'Inset', value: 'inset' },
- { text: 'Outset', value: 'outset' },
- { text: 'None', value: 'none' },
- { text: 'Hidden', value: 'hidden' }
- ]
- }
- ]
- }
- ]
- });
- const AdvTab = {
- makeTab: makeTab$2
- };
- const collect = (editor) => {
- const urlListSanitizer = ListUtils.sanitizer((item) => editor.convertURL(item.value || item.url || '', 'src'));
- const futureImageList = new Promise((completer) => {
- createImageList(editor, (imageList) => {
- completer(urlListSanitizer(imageList).map((items) => flatten([
- [{ text: 'None', value: '' }],
- items
- ])));
- });
- });
- const classList = ListUtils.sanitize(getClassList(editor));
- const hasAdvTab$1 = hasAdvTab(editor);
- const hasUploadTab$1 = hasUploadTab(editor);
- const hasUploadUrl$1 = hasUploadUrl(editor);
- const hasUploadHandler$1 = hasUploadHandler(editor);
- const image = readImageDataFromSelection(editor);
- const hasDescription$1 = hasDescription(editor);
- const hasImageTitle$1 = hasImageTitle(editor);
- const hasDimensions$1 = hasDimensions(editor);
- const hasImageCaption$1 = hasImageCaption(editor);
- const hasAccessibilityOptions = showAccessibilityOptions(editor);
- const automaticUploads = isAutomaticUploadsEnabled(editor);
- const prependURL = Optional.some(getPrependUrl(editor)).filter((preUrl) => isString(preUrl) && preUrl.length > 0);
- return futureImageList.then((imageList) => ({
- image,
- imageList,
- classList,
- hasAdvTab: hasAdvTab$1,
- hasUploadTab: hasUploadTab$1,
- hasUploadUrl: hasUploadUrl$1,
- hasUploadHandler: hasUploadHandler$1,
- hasDescription: hasDescription$1,
- hasImageTitle: hasImageTitle$1,
- hasDimensions: hasDimensions$1,
- hasImageCaption: hasImageCaption$1,
- prependURL,
- hasAccessibilityOptions,
- automaticUploads
- }));
- };
- const makeItems = (info) => {
- const imageUrl = {
- name: 'src',
- type: 'urlinput',
- filetype: 'image',
- label: 'Source',
- picker_text: 'Browse files'
- };
- const imageList = info.imageList.map((items) => ({
- name: 'images',
- type: 'listbox',
- label: 'Image list',
- items
- }));
- const imageDescription = {
- name: 'alt',
- type: 'input',
- label: 'Alternative description',
- enabled: !(info.hasAccessibilityOptions && info.image.isDecorative)
- };
- const imageTitle = {
- name: 'title',
- type: 'input',
- label: 'Image title'
- };
- const imageDimensions = {
- name: 'dimensions',
- type: 'sizeinput'
- };
- const isDecorative = {
- type: 'label',
- label: 'Accessibility',
- items: [{
- name: 'isDecorative',
- type: 'checkbox',
- label: 'Image is decorative'
- }]
- };
- // TODO: the original listbox supported styled items but bridge does not seem to support this
- const classList = info.classList.map((items) => ({
- name: 'classes',
- type: 'listbox',
- label: 'Class',
- items
- }));
- const caption = {
- type: 'label',
- label: 'Caption',
- items: [
- {
- type: 'checkbox',
- name: 'caption',
- label: 'Show caption'
- }
- ]
- };
- const getDialogContainerType = (useColumns) => useColumns ? { type: 'grid', columns: 2 } : { type: 'panel' };
- return flatten([
- [imageUrl],
- imageList.toArray(),
- info.hasAccessibilityOptions && info.hasDescription ? [isDecorative] : [],
- info.hasDescription ? [imageDescription] : [],
- info.hasImageTitle ? [imageTitle] : [],
- info.hasDimensions ? [imageDimensions] : [],
- [{
- ...getDialogContainerType(info.classList.isSome() && info.hasImageCaption),
- items: flatten([
- classList.toArray(),
- info.hasImageCaption ? [caption] : []
- ])
- }]
- ]);
- };
- const makeTab$1 = (info) => ({
- title: 'General',
- name: 'general',
- items: makeItems(info)
- });
- const MainTab = {
- makeTab: makeTab$1,
- makeItems
- };
- const makeTab = (_info) => {
- const items = [
- {
- type: 'dropzone',
- name: 'fileinput'
- }
- ];
- return {
- title: 'Upload',
- name: 'upload',
- items
- };
- };
- const UploadTab = {
- makeTab
- };
- const createState = (info) => ({
- prevImage: ListUtils.findEntry(info.imageList, info.image.src),
- prevAlt: info.image.alt,
- open: true
- });
- const fromImageData = (image) => ({
- src: {
- value: image.src,
- meta: {}
- },
- images: image.src,
- alt: image.alt,
- title: image.title,
- dimensions: {
- width: image.width,
- height: image.height
- },
- classes: image.class,
- caption: image.caption,
- style: image.style,
- vspace: image.vspace,
- border: image.border,
- hspace: image.hspace,
- borderstyle: image.borderStyle,
- fileinput: [],
- isDecorative: image.isDecorative
- });
- const toImageData = (data, removeEmptyAlt) => ({
- src: data.src.value,
- alt: (data.alt === null || data.alt.length === 0) && removeEmptyAlt ? null : data.alt,
- title: data.title,
- width: data.dimensions.width,
- height: data.dimensions.height,
- class: data.classes,
- style: data.style,
- caption: data.caption,
- hspace: data.hspace,
- vspace: data.vspace,
- border: data.border,
- borderStyle: data.borderstyle,
- isDecorative: data.isDecorative
- });
- const addPrependUrl2 = (info, srcURL) => {
- // Add the prependURL
- if (!/^(?:[a-zA-Z]+:)?\/\//.test(srcURL)) {
- return info.prependURL.bind((prependUrl) => {
- if (srcURL.substring(0, prependUrl.length) !== prependUrl) {
- return Optional.some(prependUrl + srcURL);
- }
- return Optional.none();
- });
- }
- return Optional.none();
- };
- const addPrependUrl = (info, api) => {
- const data = api.getData();
- addPrependUrl2(info, data.src.value).each((srcURL) => {
- api.setData({ src: { value: srcURL, meta: data.src.meta } });
- });
- };
- const formFillFromMeta2 = (info, data, meta) => {
- if (info.hasDescription && isString(meta.alt)) {
- data.alt = meta.alt;
- }
- if (info.hasAccessibilityOptions) {
- data.isDecorative = meta.isDecorative || data.isDecorative || false;
- }
- if (info.hasImageTitle && isString(meta.title)) {
- data.title = meta.title;
- }
- if (info.hasDimensions) {
- if (isString(meta.width)) {
- data.dimensions.width = meta.width;
- }
- if (isString(meta.height)) {
- data.dimensions.height = meta.height;
- }
- }
- if (isString(meta.class)) {
- ListUtils.findEntry(info.classList, meta.class).each((entry) => {
- data.classes = entry.value;
- });
- }
- if (info.hasImageCaption) {
- if (isBoolean(meta.caption)) {
- data.caption = meta.caption;
- }
- }
- if (info.hasAdvTab) {
- if (isString(meta.style)) {
- data.style = meta.style;
- }
- if (isString(meta.vspace)) {
- data.vspace = meta.vspace;
- }
- if (isString(meta.border)) {
- data.border = meta.border;
- }
- if (isString(meta.hspace)) {
- data.hspace = meta.hspace;
- }
- if (isString(meta.borderstyle)) {
- data.borderstyle = meta.borderstyle;
- }
- }
- };
- const formFillFromMeta = (info, api) => {
- const data = api.getData();
- const meta = data.src.meta;
- if (meta !== undefined) {
- const newData = deepMerge({}, data);
- formFillFromMeta2(info, newData, meta);
- api.setData(newData);
- }
- };
- const calculateImageSize = (helpers, info, state, api) => {
- const data = api.getData();
- const url = data.src.value;
- const meta = data.src.meta || {};
- if (!meta.width && !meta.height && info.hasDimensions) {
- if (isNotEmpty(url)) {
- helpers.imageSize(url)
- .then((size) => {
- if (state.open) {
- api.setData({ dimensions: size });
- }
- })
- // eslint-disable-next-line no-console
- .catch((e) => console.error(e));
- }
- else {
- api.setData({ dimensions: { width: '', height: '' } });
- }
- }
- };
- const updateImagesDropdown = (info, state, api) => {
- const data = api.getData();
- const image = ListUtils.findEntry(info.imageList, data.src.value);
- state.prevImage = image;
- api.setData({ images: image.map((entry) => entry.value).getOr('') });
- };
- const changeSrc = (helpers, info, state, api) => {
- addPrependUrl(info, api);
- formFillFromMeta(info, api);
- calculateImageSize(helpers, info, state, api);
- updateImagesDropdown(info, state, api);
- };
- const changeImages = (helpers, info, state, api) => {
- const data = api.getData();
- const image = ListUtils.findEntry(info.imageList, data.images);
- image.each((img) => {
- const updateAlt = data.alt === '' || state.prevImage.map((image) => image.text === data.alt).getOr(false);
- if (updateAlt) {
- if (img.value === '') {
- api.setData({ src: img, alt: state.prevAlt });
- }
- else {
- api.setData({ src: img, alt: img.text });
- }
- }
- else {
- api.setData({ src: img });
- }
- });
- state.prevImage = image;
- changeSrc(helpers, info, state, api);
- };
- const changeFileInput = (helpers, info, state, api) => {
- const data = api.getData();
- api.block('Uploading image'); // What msg do we pass to the lock?
- head(data.fileinput)
- .fold(() => {
- api.unblock();
- }, (file) => {
- const blobUri = URL.createObjectURL(file);
- const finalize = () => {
- api.unblock();
- URL.revokeObjectURL(blobUri);
- };
- const updateSrcAndSwitchTab = (url) => {
- api.setData({ src: { value: url, meta: {} } });
- api.showTab('general');
- changeSrc(helpers, info, state, api);
- api.focus('src');
- };
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- blobToDataUri(file).then((dataUrl) => {
- const blobInfo = helpers.createBlobCache(file, blobUri, dataUrl);
- if (info.automaticUploads) {
- helpers.uploadImage(blobInfo).then((result) => {
- updateSrcAndSwitchTab(result.url);
- finalize();
- }).catch((err) => {
- finalize();
- helpers.alertErr(err, () => {
- api.focus('fileinput');
- });
- });
- }
- else {
- helpers.addToBlobCache(blobInfo);
- updateSrcAndSwitchTab(blobInfo.blobUri());
- api.unblock();
- }
- });
- });
- };
- const changeHandler = (helpers, info, state) => (api, evt) => {
- if (evt.name === 'src') {
- changeSrc(helpers, info, state, api);
- }
- else if (evt.name === 'images') {
- changeImages(helpers, info, state, api);
- }
- else if (evt.name === 'alt') {
- state.prevAlt = api.getData().alt;
- }
- else if (evt.name === 'fileinput') {
- changeFileInput(helpers, info, state, api);
- }
- else if (evt.name === 'isDecorative') {
- api.setEnabled('alt', !api.getData().isDecorative);
- }
- };
- const closeHandler = (state) => () => {
- state.open = false;
- };
- const makeDialogBody = (info) => {
- if (info.hasAdvTab || info.hasUploadUrl || info.hasUploadHandler) {
- const tabPanel = {
- type: 'tabpanel',
- tabs: flatten([
- [MainTab.makeTab(info)],
- info.hasAdvTab ? [AdvTab.makeTab(info)] : [],
- info.hasUploadTab && (info.hasUploadUrl || info.hasUploadHandler) ? [UploadTab.makeTab(info)] : []
- ])
- };
- return tabPanel;
- }
- else {
- const panel = {
- type: 'panel',
- items: MainTab.makeItems(info)
- };
- return panel;
- }
- };
- const submitHandler = (editor, info, helpers) => (api) => {
- const data = deepMerge(fromImageData(info.image), api.getData());
- // The data architecture relies on passing everything through the style field for validation.
- // Since the style field was removed that process must be simulated on submit.
- const finalData = {
- ...data,
- style: getStyleValue(helpers.normalizeCss, toImageData(data, false))
- };
- editor.execCommand('mceUpdateImage', false, toImageData(finalData, info.hasAccessibilityOptions));
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- editor.editorUpload.uploadImagesAuto();
- api.close();
- };
- const imageSize = (editor) => (url) => {
- // If the URL isn't safe then don't attempt to load it to get the sizes
- if (!isSafeImageUrl(editor, url)) {
- return Promise.resolve({ width: '', height: '' });
- }
- else {
- return getImageSize(editor.documentBaseURI.toAbsolute(url)).then((dimensions) => ({
- width: String(dimensions.width),
- height: String(dimensions.height)
- }));
- }
- };
- const createBlobCache = (editor) => (file, blobUri, dataUrl) => {
- var _a;
- return editor.editorUpload.blobCache.create({
- blob: file,
- blobUri,
- name: (_a = file.name) === null || _a === void 0 ? void 0 : _a.replace(/\.[^\.]+$/, ''),
- filename: file.name,
- base64: dataUrl.split(',')[1]
- });
- };
- const addToBlobCache = (editor) => (blobInfo) => {
- editor.editorUpload.blobCache.add(blobInfo);
- };
- const alertErr = (editor) => (message, callback) => {
- editor.windowManager.alert(message, callback);
- };
- const normalizeCss = (editor) => (cssText) => normalizeCss$1(editor, cssText);
- const parseStyle = (editor) => (cssText) => editor.dom.parseStyle(cssText);
- const serializeStyle = (editor) => (stylesArg, name) => editor.dom.serializeStyle(stylesArg, name);
- const uploadImage = (editor) => (blobInfo) => global$1(editor).upload([blobInfo], false).then((results) => {
- var _a;
- if (results.length === 0) {
- return Promise.reject('Failed to upload image');
- }
- else if (results[0].status === false) {
- return Promise.reject((_a = results[0].error) === null || _a === void 0 ? void 0 : _a.message);
- }
- else {
- return results[0];
- }
- });
- const Dialog = (editor) => {
- const helpers = {
- imageSize: imageSize(editor),
- addToBlobCache: addToBlobCache(editor),
- createBlobCache: createBlobCache(editor),
- alertErr: alertErr(editor),
- normalizeCss: normalizeCss(editor),
- parseStyle: parseStyle(editor),
- serializeStyle: serializeStyle(editor),
- uploadImage: uploadImage(editor)
- };
- const open = () => {
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
- collect(editor)
- .then((info) => {
- const state = createState(info);
- return {
- title: 'Insert/Edit Image',
- size: 'normal',
- body: makeDialogBody(info),
- buttons: [
- {
- type: 'cancel',
- name: 'cancel',
- text: 'Cancel'
- },
- {
- type: 'submit',
- name: 'save',
- text: 'Save',
- primary: true
- }
- ],
- initialData: fromImageData(info.image),
- onSubmit: submitHandler(editor, info, helpers),
- onChange: changeHandler(helpers, info, state),
- onClose: closeHandler(state)
- };
- })
- .then(editor.windowManager.open);
- };
- return {
- open
- };
- };
- const register$1 = (editor) => {
- editor.addCommand('mceImage', Dialog(editor).open);
- // TODO: This command is likely to be short lived we only need it until we expose the rtc model though a new api so it shouldn't be documented
- // it's just a command since that is a convenient method for the rtc plugin to override the default dom mutation behaviour
- editor.addCommand('mceUpdateImage', (_ui, data) => {
- editor.undoManager.transact(() => insertOrUpdateImage(editor, data));
- });
- };
- const hasImageClass = (node) => {
- const className = node.attr('class');
- return isNonNullable(className) && /\bimage\b/.test(className);
- };
- const toggleContentEditableState = (state) => (nodes) => {
- let i = nodes.length;
- const toggleContentEditable = (node) => {
- node.attr('contenteditable', state ? 'true' : null);
- };
- while (i--) {
- const node = nodes[i];
- if (hasImageClass(node)) {
- node.attr('contenteditable', state ? 'false' : null);
- global.each(node.getAll('figcaption'), toggleContentEditable);
- }
- }
- };
- const setup = (editor) => {
- editor.on('PreInit', () => {
- editor.parser.addNodeFilter('figure', toggleContentEditableState(true));
- editor.serializer.addNodeFilter('figure', toggleContentEditableState(false));
- });
- };
- const onSetupEditable = (editor) => (api) => {
- const nodeChanged = () => {
- api.setEnabled(editor.selection.isEditable());
- };
- editor.on('NodeChange', nodeChanged);
- nodeChanged();
- return () => {
- editor.off('NodeChange', nodeChanged);
- };
- };
- const register = (editor) => {
- editor.ui.registry.addToggleButton('image', {
- icon: 'image',
- tooltip: 'Insert/edit image',
- onAction: Dialog(editor).open,
- onSetup: (buttonApi) => {
- // Set the initial state and then bind to selection changes to update the state when the selection changes
- buttonApi.setActive(isNonNullable(getSelectedImage(editor)));
- const unbindSelectorChanged = editor.selection.selectorChangedWithUnbind('img:not([data-mce-object]):not([data-mce-placeholder]),figure.image', buttonApi.setActive).unbind;
- const unbindEditable = onSetupEditable(editor)(buttonApi);
- return () => {
- unbindSelectorChanged();
- unbindEditable();
- };
- }
- });
- editor.ui.registry.addMenuItem('image', {
- icon: 'image',
- text: 'Image...',
- onAction: Dialog(editor).open,
- onSetup: onSetupEditable(editor)
- });
- editor.ui.registry.addContextMenu('image', {
- update: (element) => editor.selection.isEditable() && (isFigure(element) || (isImage(element) && !isPlaceholderImage(element))) ? ['image'] : []
- });
- };
- var Plugin = () => {
- global$4.add('image', (editor) => {
- register$2(editor);
- setup(editor);
- register(editor);
- register$1(editor);
- });
- };
- Plugin();
- /** *****
- * DO NOT EXPORT ANYTHING
- *
- * IF YOU DO ROLLUP WILL LEAVE A GLOBAL ON THE PAGE
- *******/
- })();
|