1 | var __defProp = Object.defineProperty;
|
2 | var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
3 | var __defNormalProp = (obj, key2, value) => key2 in obj ? __defProp(obj, key2, { enumerable: true, configurable: true, writable: true, value }) : obj[key2] = value;
|
4 | var __decorateClass = (decorators, target, key2, kind) => {
|
5 | var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key2) : target;
|
6 | for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
7 | if (decorator = decorators[i])
|
8 | result = (kind ? decorator(target, key2, result) : decorator(result)) || result;
|
9 | if (kind && result)
|
10 | __defProp(target, key2, result);
|
11 | return result;
|
12 | };
|
13 | var __publicField = (obj, key2, value) => {
|
14 | __defNormalProp(obj, typeof key2 !== "symbol" ? key2 + "" : key2, value);
|
15 | return value;
|
16 | };
|
17 | var __accessCheck = (obj, member, msg) => {
|
18 | if (!member.has(obj))
|
19 | throw TypeError("Cannot " + msg);
|
20 | };
|
21 | var __privateGet = (obj, member, getter) => {
|
22 | __accessCheck(obj, member, "read from private field");
|
23 | return getter ? getter.call(obj) : member.get(obj);
|
24 | };
|
25 | var __privateAdd = (obj, member, value) => {
|
26 | if (member.has(obj))
|
27 | throw TypeError("Cannot add the same private member more than once");
|
28 | member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
29 | };
|
30 | var __privateSet = (obj, member, value, setter) => {
|
31 | __accessCheck(obj, member, "write to private field");
|
32 | setter ? setter.call(obj, value) : member.set(obj, value);
|
33 | return value;
|
34 | };
|
35 |
|
36 |
|
37 | import { cx, object as object3 } from "@remirror/core-helpers";
|
38 |
|
39 |
|
40 | import {
|
41 | __INTERNAL_REMIRROR_IDENTIFIER_KEY__ as __INTERNAL_REMIRROR_IDENTIFIER_KEY__2,
|
42 | ErrorConstant as ErrorConstant3,
|
43 | ExtensionPriority as ExtensionPriority2,
|
44 | RemirrorIdentifier as RemirrorIdentifier2
|
45 | } from "@remirror/core-constants";
|
46 | import { assertGet, freeze as freeze2, invariant as invariant3, pascalCase, uniqueBy } from "@remirror/core-helpers";
|
47 | import { isIdentifierOfType, isRemirrorType } from "@remirror/core-utils";
|
48 |
|
49 |
|
50 | import {
|
51 | ErrorConstant as ErrorConstant2,
|
52 | ExtensionPriority
|
53 | } from "@remirror/core-constants";
|
54 | import {
|
55 | deepMerge,
|
56 | invariant as invariant2,
|
57 | isEmptyArray,
|
58 | isFunction,
|
59 | keys as keys2,
|
60 | noop,
|
61 | object as object2,
|
62 | omit,
|
63 | sort
|
64 | } from "@remirror/core-helpers";
|
65 | import { environment } from "@remirror/core-utils";
|
66 |
|
67 |
|
68 | import { ErrorConstant } from "@remirror/core-constants";
|
69 | import { freeze, invariant, keys, object } from "@remirror/core-helpers";
|
70 | function defaultEquals(valueA, valueB) {
|
71 | return valueA === valueB;
|
72 | }
|
73 | function getChangedOptions(props) {
|
74 | const { previousOptions, update, equals = defaultEquals } = props;
|
75 | const next = freeze({ ...previousOptions, ...update });
|
76 | const changes = object();
|
77 | const optionKeys = keys(previousOptions);
|
78 | for (const key2 of optionKeys) {
|
79 | const previousValue = previousOptions[key2];
|
80 | const value = next[key2];
|
81 | if (equals(previousValue, value)) {
|
82 | changes[key2] = { changed: false };
|
83 | continue;
|
84 | }
|
85 | changes[key2] = { changed: true, previousValue, value };
|
86 | }
|
87 | const pickChanged = (keys3) => {
|
88 | const picked = object();
|
89 | for (const key2 of keys3) {
|
90 | const item = changes[key2];
|
91 | if (item == null ? void 0 : item.changed) {
|
92 | picked[key2] = item.value;
|
93 | }
|
94 | }
|
95 | return picked;
|
96 | };
|
97 | return { changes: freeze(changes), options: next, pickChanged };
|
98 | }
|
99 | var codeLabelMap = {
|
100 | [ErrorConstant.DUPLICATE_HELPER_NAMES]: "helper method",
|
101 | [ErrorConstant.DUPLICATE_COMMAND_NAMES]: "command method"
|
102 | };
|
103 | function throwIfNameNotUnique(props) {
|
104 | const { name, set, code } = props;
|
105 | const label = codeLabelMap[code];
|
106 | invariant(!set.has(name), {
|
107 | code,
|
108 | message: `There is a naming conflict for the name: ${name} used in this '${label}'. Please rename or remove from the editor to avoid runtime errors.`
|
109 | });
|
110 | set.add(name);
|
111 | }
|
112 |
|
113 | // packages/remirror__core/src/extension/base-class.ts
|
114 | var IGNORE = "__IGNORE__";
|
115 | var GENERAL_OPTIONS = "__ALL__";
|
116 | var BaseClass = class {
|
117 | constructor(defaultOptions2, ...[options]) {
|
118 | __publicField(this, "~O", {});
|
119 | __publicField(this, "_initialOptions");
|
120 | __publicField(this, "_dynamicKeys");
|
121 | __publicField(this, "_options");
|
122 | __publicField(this, "_mappedHandlers");
|
123 | __publicField(this, "onAddCustomHandler");
|
124 | this._mappedHandlers = object2();
|
125 | this.populateMappedHandlers();
|
126 | this._options = this._initialOptions = deepMerge(defaultOptions2, this.constructor.defaultOptions, options != null ? options : object2(), this.createDefaultHandlerOptions());
|
127 | this._dynamicKeys = this.getDynamicKeys();
|
128 | this.init();
|
129 | }
|
130 | get options() {
|
131 | return this._options;
|
132 | }
|
133 | get dynamicKeys() {
|
134 | return this._dynamicKeys;
|
135 | }
|
136 | get initialOptions() {
|
137 | return this._initialOptions;
|
138 | }
|
139 | init() {
|
140 | }
|
141 | getDynamicKeys() {
|
142 | const dynamicKeys = [];
|
143 | const { customHandlerKeys, handlerKeys, staticKeys } = this.constructor;
|
144 | for (const key2 of keys2(this._options)) {
|
145 | if (staticKeys.includes(key2) || handlerKeys.includes(key2) || customHandlerKeys.includes(key2)) {
|
146 | continue;
|
147 | }
|
148 | dynamicKeys.push(key2);
|
149 | }
|
150 | return dynamicKeys;
|
151 | }
|
152 | ensureAllKeysAreDynamic(update) {
|
153 | if (environment.isProduction) {
|
154 | return;
|
155 | }
|
156 | const invalid = [];
|
157 | for (const key2 of keys2(update)) {
|
158 | if (this._dynamicKeys.includes(key2)) {
|
159 | continue;
|
160 | }
|
161 | invalid.push(key2);
|
162 | }
|
163 | invariant2(isEmptyArray(invalid), {
|
164 | code: ErrorConstant2.INVALID_SET_EXTENSION_OPTIONS,
|
165 | message: `Invalid properties passed into the 'setOptions()' method: ${JSON.stringify(invalid)}.`
|
166 | });
|
167 | }
|
168 | setOptions(update) {
|
169 | var _a;
|
170 | const previousOptions = this.getDynamicOptions();
|
171 | this.ensureAllKeysAreDynamic(update);
|
172 | const { changes, options, pickChanged } = getChangedOptions({
|
173 | previousOptions,
|
174 | update
|
175 | });
|
176 | this.updateDynamicOptions(options);
|
177 | (_a = this.onSetOptions) == null ? void 0 : _a.call(this, {
|
178 | reason: "set",
|
179 | changes,
|
180 | options,
|
181 | pickChanged,
|
182 | initialOptions: this._initialOptions
|
183 | });
|
184 | }
|
185 | resetOptions() {
|
186 | var _a;
|
187 | const previousOptions = this.getDynamicOptions();
|
188 | const { changes, options, pickChanged } = getChangedOptions({
|
189 | previousOptions,
|
190 | update: this._initialOptions
|
191 | });
|
192 | this.updateDynamicOptions(options);
|
193 | (_a = this.onSetOptions) == null ? void 0 : _a.call(this, {
|
194 | reason: "reset",
|
195 | options,
|
196 | changes,
|
197 | pickChanged,
|
198 | initialOptions: this._initialOptions
|
199 | });
|
200 | }
|
201 | getDynamicOptions() {
|
202 | return omit(this._options, [
|
203 | ...this.constructor.customHandlerKeys,
|
204 | ...this.constructor.handlerKeys
|
205 | ]);
|
206 | }
|
207 | updateDynamicOptions(options) {
|
208 | this._options = { ...this._options, ...options };
|
209 | }
|
210 | populateMappedHandlers() {
|
211 | for (const key2 of this.constructor.handlerKeys) {
|
212 | this._mappedHandlers[key2] = [];
|
213 | }
|
214 | }
|
215 | createDefaultHandlerOptions() {
|
216 | const methods = object2();
|
217 | for (const key2 of this.constructor.handlerKeys) {
|
218 | methods[key2] = (...args) => {
|
219 | var _a;
|
220 | const { handlerKeyOptions } = this.constructor;
|
221 | const reducer = (_a = handlerKeyOptions[key2]) == null ? void 0 : _a.reducer;
|
222 | let returnValue = reducer == null ? void 0 : reducer.getDefault(...args);
|
223 | for (const [, handler] of this._mappedHandlers[key2]) {
|
224 | const value = handler(...args);
|
225 | returnValue = reducer ? reducer.accumulator(returnValue, value, ...args) : value;
|
226 | if (shouldReturnEarly(handlerKeyOptions, returnValue, key2)) {
|
227 | return returnValue;
|
228 | }
|
229 | }
|
230 | return returnValue;
|
231 | };
|
232 | }
|
233 | return methods;
|
234 | }
|
235 | addHandler(key2, method, priority = ExtensionPriority.Default) {
|
236 | this._mappedHandlers[key2].push([priority, method]);
|
237 | this.sortHandlers(key2);
|
238 | return () => this._mappedHandlers[key2] = this._mappedHandlers[key2].filter(([, handler]) => handler !== method);
|
239 | }
|
240 | hasHandlers(key2) {
|
241 | var _a;
|
242 | return ((_a = this._mappedHandlers[key2]) != null ? _a : []).length > 0;
|
243 | }
|
244 | sortHandlers(key2) {
|
245 | this._mappedHandlers[key2] = sort(this._mappedHandlers[key2], ([a], [z]) => z - a);
|
246 | }
|
247 | addCustomHandler(key2, value) {
|
248 | var _a, _b;
|
249 | return (_b = (_a = this.onAddCustomHandler) == null ? void 0 : _a.call(this, { [key2]: value })) != null ? _b : noop;
|
250 | }
|
251 | };
|
252 | __publicField(BaseClass, "defaultOptions", {});
|
253 | __publicField(BaseClass, "staticKeys", []);
|
254 | __publicField(BaseClass, "handlerKeys", []);
|
255 | __publicField(BaseClass, "handlerKeyOptions", {});
|
256 | __publicField(BaseClass, "customHandlerKeys", []);
|
257 | function shouldReturnEarly(handlerKeyOptions, returnValue, handlerKey) {
|
258 | const { [GENERAL_OPTIONS]: generalOptions } = handlerKeyOptions;
|
259 | const handlerOptions = handlerKeyOptions[handlerKey];
|
260 | if (!generalOptions && !handlerOptions) {
|
261 | return false;
|
262 | }
|
263 | if (handlerOptions && handlerOptions.earlyReturnValue !== IGNORE && (isFunction(handlerOptions.earlyReturnValue) ? handlerOptions.earlyReturnValue(returnValue) === true : returnValue === handlerOptions.earlyReturnValue)) {
|
264 | return true;
|
265 | }
|
266 | if (generalOptions && generalOptions.earlyReturnValue !== IGNORE && (isFunction(generalOptions.earlyReturnValue) ? generalOptions.earlyReturnValue(returnValue) === true : returnValue === generalOptions.earlyReturnValue)) {
|
267 | return true;
|
268 | }
|
269 | return false;
|
270 | }
|
271 |
|
272 |
|
273 | var Extension = class extends BaseClass {
|
274 | constructor(...args) {
|
275 | super(defaultOptions, ...args);
|
276 | __publicField(this, "priorityOverride");
|
277 | __publicField(this, "_extensions");
|
278 | __publicField(this, "extensionMap");
|
279 | __publicField(this, "_store");
|
280 | __publicField(this, "~E", {});
|
281 | this._extensions = uniqueBy(this.createExtensions(), (extension2) => extension2.constructor);
|
282 | this.extensionMap = new Map();
|
283 | for (const extension2 of this._extensions) {
|
284 | this.extensionMap.set(extension2.constructor, extension2);
|
285 | }
|
286 | }
|
287 | get priority() {
|
288 | var _a, _b;
|
289 | return (_b = (_a = this.priorityOverride) != null ? _a : this.options.priority) != null ? _b : this.constructor.defaultPriority;
|
290 | }
|
291 | get constructorName() {
|
292 | return `${pascalCase(this.name)}Extension`;
|
293 | }
|
294 | get store() {
|
295 | invariant3(this._store, {
|
296 | code: ErrorConstant3.MANAGER_PHASE_ERROR,
|
297 | message: `An error occurred while attempting to access the 'extension.store' when the Manager has not yet set created the lifecycle methods.`
|
298 | });
|
299 | return freeze2(this._store, { requireKeys: true });
|
300 | }
|
301 | get extensions() {
|
302 | return this._extensions;
|
303 | }
|
304 | replaceChildExtension(constructor, extension2) {
|
305 | if (!this.extensionMap.has(constructor)) {
|
306 | return;
|
307 | }
|
308 | this.extensionMap.set(constructor, extension2);
|
309 | this._extensions = this.extensions.map((currentExtension) => extension2.constructor === constructor ? extension2 : currentExtension);
|
310 | }
|
311 | createExtensions() {
|
312 | return [];
|
313 | }
|
314 | getExtension(Constructor) {
|
315 | const extension2 = this.extensionMap.get(Constructor);
|
316 | invariant3(extension2, {
|
317 | code: ErrorConstant3.INVALID_GET_EXTENSION,
|
318 | message: `'${Constructor.name}' does not exist within the preset: '${this.name}'`
|
319 | });
|
320 | return extension2;
|
321 | }
|
322 | isOfType(Constructor) {
|
323 | return this.constructor === Constructor;
|
324 | }
|
325 | setStore(store) {
|
326 | if (this._store) {
|
327 | return;
|
328 | }
|
329 | this._store = store;
|
330 | }
|
331 | clone(...args) {
|
332 | return new this.constructor(...args);
|
333 | }
|
334 | setPriority(priority) {
|
335 | this.priorityOverride = priority;
|
336 | }
|
337 | };
|
338 | __publicField(Extension, "defaultPriority", ExtensionPriority2.Default);
|
339 | var PlainExtension = class extends Extension {
|
340 | static get [__INTERNAL_REMIRROR_IDENTIFIER_KEY__2]() {
|
341 | return RemirrorIdentifier2.PlainExtensionConstructor;
|
342 | }
|
343 | get [__INTERNAL_REMIRROR_IDENTIFIER_KEY__2]() {
|
344 | return RemirrorIdentifier2.PlainExtension;
|
345 | }
|
346 | };
|
347 | var MarkExtension = class extends Extension {
|
348 | static get [__INTERNAL_REMIRROR_IDENTIFIER_KEY__2]() {
|
349 | return RemirrorIdentifier2.MarkExtensionConstructor;
|
350 | }
|
351 | get [__INTERNAL_REMIRROR_IDENTIFIER_KEY__2]() {
|
352 | return RemirrorIdentifier2.MarkExtension;
|
353 | }
|
354 | get type() {
|
355 | return assertGet(this.store.schema.marks, this.name);
|
356 | }
|
357 | constructor(...args) {
|
358 | super(...args);
|
359 | }
|
360 | };
|
361 | __publicField(MarkExtension, "disableExtraAttributes", false);
|
362 | var NodeExtension = class extends Extension {
|
363 | static get [__INTERNAL_REMIRROR_IDENTIFIER_KEY__2]() {
|
364 | return RemirrorIdentifier2.NodeExtensionConstructor;
|
365 | }
|
366 | get [__INTERNAL_REMIRROR_IDENTIFIER_KEY__2]() {
|
367 | return RemirrorIdentifier2.NodeExtension;
|
368 | }
|
369 | get type() {
|
370 | return assertGet(this.store.schema.nodes, this.name);
|
371 | }
|
372 | constructor(...args) {
|
373 | super(...args);
|
374 | }
|
375 | };
|
376 | __publicField(NodeExtension, "disableExtraAttributes", false);
|
377 | var defaultOptions = {
|
378 | priority: void 0,
|
379 | extraAttributes: {},
|
380 | disableExtraAttributes: false,
|
381 | exclude: {}
|
382 | };
|
383 | function mutateDefaultExtensionOptions(mutatorMethod) {
|
384 | mutatorMethod(defaultOptions);
|
385 | }
|
386 | function isExtension(value) {
|
387 | return isRemirrorType(value) && isIdentifierOfType(value, [
|
388 | RemirrorIdentifier2.PlainExtension,
|
389 | RemirrorIdentifier2.MarkExtension,
|
390 | RemirrorIdentifier2.NodeExtension
|
391 | ]);
|
392 | }
|
393 | function isExtensionConstructor(value) {
|
394 | return isRemirrorType(value) && isIdentifierOfType(value, [
|
395 | RemirrorIdentifier2.PlainExtensionConstructor,
|
396 | RemirrorIdentifier2.MarkExtensionConstructor,
|
397 | RemirrorIdentifier2.NodeExtensionConstructor
|
398 | ]);
|
399 | }
|
400 | function isPlainExtension(value) {
|
401 | return isRemirrorType(value) && isIdentifierOfType(value, RemirrorIdentifier2.PlainExtension);
|
402 | }
|
403 | function isNodeExtension(value) {
|
404 | return isRemirrorType(value) && isIdentifierOfType(value, RemirrorIdentifier2.NodeExtension);
|
405 | }
|
406 | function isMarkExtension(value) {
|
407 | return isRemirrorType(value) && isIdentifierOfType(value, RemirrorIdentifier2.MarkExtension);
|
408 | }
|
409 |
|
410 |
|
411 | import { Cast } from "@remirror/core-helpers";
|
412 | function extension(options) {
|
413 | return (ReadonlyConstructor) => {
|
414 | const {
|
415 | defaultOptions: defaultOptions2,
|
416 | customHandlerKeys,
|
417 | handlerKeys,
|
418 | staticKeys,
|
419 | defaultPriority,
|
420 | handlerKeyOptions,
|
421 | ...rest
|
422 | } = options;
|
423 | const Constructor = Cast(ReadonlyConstructor);
|
424 | if (defaultOptions2) {
|
425 | Constructor.defaultOptions = defaultOptions2;
|
426 | }
|
427 | if (defaultPriority) {
|
428 | Constructor.defaultPriority = defaultPriority;
|
429 | }
|
430 | if (handlerKeyOptions) {
|
431 | Constructor.handlerKeyOptions = handlerKeyOptions;
|
432 | }
|
433 | Constructor.staticKeys = staticKeys != null ? staticKeys : [];
|
434 | Constructor.handlerKeys = handlerKeys != null ? handlerKeys : [];
|
435 | Constructor.customHandlerKeys = customHandlerKeys != null ? customHandlerKeys : [];
|
436 | for (const [key2, value] of Object.entries(rest)) {
|
437 | if (Constructor[key2]) {
|
438 | continue;
|
439 | }
|
440 | Constructor[key2] = value;
|
441 | }
|
442 | return Cast(Constructor);
|
443 | };
|
444 | }
|
445 | var extensionDecorator = extension;
|
446 |
|
447 |
|
448 | var AttributesExtension = class extends PlainExtension {
|
449 | constructor() {
|
450 | super(...arguments);
|
451 | __publicField(this, "attributeList", []);
|
452 | __publicField(this, "attributeObject", object3());
|
453 | __publicField(this, "updateAttributes", (triggerUpdate = true) => {
|
454 | this.transformAttributes();
|
455 | if (triggerUpdate) {
|
456 | this.store.commands.forceUpdate("attributes");
|
457 | }
|
458 | });
|
459 | }
|
460 | get name() {
|
461 | return "attributes";
|
462 | }
|
463 | onCreate() {
|
464 | this.transformAttributes();
|
465 | this.store.setExtensionStore("updateAttributes", this.updateAttributes);
|
466 | }
|
467 | transformAttributes() {
|
468 | var _a, _b, _c, _d;
|
469 | this.attributeObject = object3();
|
470 | if ((_a = this.store.managerSettings.exclude) == null ? void 0 : _a.attributes) {
|
471 | this.store.setStoreKey("attributes", this.attributeObject);
|
472 | return;
|
473 | }
|
474 | this.attributeList = [];
|
475 | for (const extension2 of this.store.extensions) {
|
476 | if ((_b = extension2.options.exclude) == null ? void 0 : _b.attributes) {
|
477 | continue;
|
478 | }
|
479 | const createdAttributes = (_c = extension2.createAttributes) == null ? void 0 : _c.call(extension2);
|
480 | const attributes = {
|
481 | ...createdAttributes,
|
482 | class: cx(...(_d = extension2.classNames) != null ? _d : [], createdAttributes == null ? void 0 : createdAttributes.class)
|
483 | };
|
484 | this.attributeList.unshift(attributes);
|
485 | }
|
486 | for (const attributes of this.attributeList) {
|
487 | this.attributeObject = {
|
488 | ...this.attributeObject,
|
489 | ...attributes,
|
490 | class: cx(this.attributeObject.class, attributes.class)
|
491 | };
|
492 | }
|
493 | this.store.setStoreKey("attributes", this.attributeObject);
|
494 | }
|
495 | };
|
496 |
|
497 |
|
498 | function helper(options = {}) {
|
499 | return (target, propertyKey, _descriptor) => {
|
500 | var _a;
|
501 | ((_a = target.decoratedHelpers) != null ? _a : target.decoratedHelpers = {})[propertyKey] = options;
|
502 | };
|
503 | }
|
504 | function command(options = {}) {
|
505 | return (target, propertyKey, _descriptor) => {
|
506 | var _a;
|
507 | ((_a = target.decoratedCommands) != null ? _a : target.decoratedCommands = {})[propertyKey] = options;
|
508 | };
|
509 | }
|
510 | function keyBinding(options) {
|
511 | return (target, propertyKey, _descriptor) => {
|
512 | var _a;
|
513 | ((_a = target.decoratedKeybindings) != null ? _a : target.decoratedKeybindings = {})[propertyKey] = options;
|
514 | };
|
515 | }
|
516 |
|
517 |
|
518 | import { pick } from "@remirror/core-helpers";
|
519 |
|
520 |
|
521 | import { ErrorConstant as ErrorConstant5, ExtensionPriority as ExtensionPriority3, NamedShortcut } from "@remirror/core-constants";
|
522 | import {
|
523 | entries as entries2,
|
524 | invariant as invariant5,
|
525 | isEmptyArray as isEmptyArray2,
|
526 | isEmptyObject,
|
527 | isString as isString2,
|
528 | object as object4,
|
529 | uniqueArray
|
530 | } from "@remirror/core-helpers";
|
531 | import {
|
532 | environment as environment2,
|
533 | getMarkRange,
|
534 | getTextSelection as getTextSelection2,
|
535 | htmlToProsemirrorNode,
|
536 | isEmptyBlockNode,
|
537 | isProsemirrorFragment,
|
538 | isProsemirrorNode,
|
539 | isTextSelection,
|
540 | removeMark,
|
541 | replaceText,
|
542 | setBlockType,
|
543 | toggleBlockItem,
|
544 | toggleWrap,
|
545 | wrapIn
|
546 | } from "@remirror/core-utils";
|
547 | import { CoreMessages as Messages } from "@remirror/messages";
|
548 | import { Mark } from "@remirror/pm/model";
|
549 | import { TextSelection } from "@remirror/pm/state";
|
550 |
|
551 |
|
552 | import { ErrorConstant as ErrorConstant4 } from "@remirror/core-constants";
|
553 | import {
|
554 | assertGet as assertGet2,
|
555 | entries,
|
556 | invariant as invariant4,
|
557 | isFunction as isFunction2,
|
558 | isPromise,
|
559 | isString
|
560 | } from "@remirror/core-helpers";
|
561 | import { convertCommand, getCursor, getTextSelection, isMarkActive } from "@remirror/core-utils";
|
562 | import { toggleMark as originalToggleMark } from "@remirror/pm/commands";
|
563 | function isDelayedValue(value) {
|
564 | return isFunction2(value) || isPromise(value);
|
565 | }
|
566 | function delayedCommand({
|
567 | immediate,
|
568 | promise,
|
569 | onDone,
|
570 | onFail
|
571 | }) {
|
572 | return (props) => {
|
573 | const { view } = props;
|
574 | if ((immediate == null ? void 0 : immediate(props)) === false) {
|
575 | return false;
|
576 | }
|
577 | if (!view) {
|
578 | return true;
|
579 | }
|
580 | const deferred = isFunction2(promise) ? promise() : promise;
|
581 | deferred.then((value) => {
|
582 | onDone({ state: view.state, tr: view.state.tr, dispatch: view.dispatch, view, value });
|
583 | }).catch(() => {
|
584 | onFail == null ? void 0 : onFail({ state: view.state, tr: view.state.tr, dispatch: view.dispatch, view });
|
585 | });
|
586 | return true;
|
587 | };
|
588 | }
|
589 | var DelayedCommand = class {
|
590 | constructor(promiseCreator) {
|
591 | this.promiseCreator = promiseCreator;
|
592 | __publicField(this, "failureHandlers", []);
|
593 | __publicField(this, "successHandlers", []);
|
594 | __publicField(this, "validateHandlers", []);
|
595 | __publicField(this, "generateCommand", () => {
|
596 | return (props) => {
|
597 | let isValid = true;
|
598 | const { view, tr, dispatch } = props;
|
599 | if (!view) {
|
600 | return false;
|
601 | }
|
602 | for (const handler of this.validateHandlers) {
|
603 | if (!handler({ ...props, dispatch: () => {
|
604 | } })) {
|
605 | isValid = false;
|
606 | break;
|
607 | }
|
608 | }
|
609 | if (!dispatch || !isValid) {
|
610 | return isValid;
|
611 | }
|
612 | const deferred = this.promiseCreator(props);
|
613 | deferred.then((value) => {
|
614 | this.runHandlers(this.successHandlers, {
|
615 | value,
|
616 | state: view.state,
|
617 | tr: view.state.tr,
|
618 | dispatch: view.dispatch,
|
619 | view
|
620 | });
|
621 | }).catch((error) => {
|
622 | this.runHandlers(this.failureHandlers, {
|
623 | error,
|
624 | state: view.state,
|
625 | tr: view.state.tr,
|
626 | dispatch: view.dispatch,
|
627 | view
|
628 | });
|
629 | });
|
630 | dispatch(tr);
|
631 | return true;
|
632 | };
|
633 | });
|
634 | }
|
635 | validate(handler, method = "push") {
|
636 | this.validateHandlers[method](handler);
|
637 | return this;
|
638 | }
|
639 | success(handler, method = "push") {
|
640 | this.successHandlers[method](handler);
|
641 | return this;
|
642 | }
|
643 | failure(handler, method = "push") {
|
644 | this.failureHandlers[method](handler);
|
645 | return this;
|
646 | }
|
647 | runHandlers(handlers, param) {
|
648 | var _a;
|
649 | for (const handler of handlers) {
|
650 | if (!handler({ ...param, dispatch: () => {
|
651 | } })) {
|
652 | break;
|
653 | }
|
654 | }
|
655 | (_a = param.dispatch) == null ? void 0 : _a.call(param, param.tr);
|
656 | }
|
657 | };
|
658 | function toggleMark(props) {
|
659 | const { type, attrs, range, selection } = props;
|
660 | return (props2) => {
|
661 | var _a;
|
662 | const { dispatch, tr, state } = props2;
|
663 | const markType = isString(type) ? state.schema.marks[type] : type;
|
664 | invariant4(markType, {
|
665 | code: ErrorConstant4.SCHEMA,
|
666 | message: `Mark type: ${type} does not exist on the current schema.`
|
667 | });
|
668 | if (range || selection) {
|
669 | const { from, to } = getTextSelection((_a = selection != null ? selection : range) != null ? _a : tr.selection, tr.doc);
|
670 | isMarkActive({ trState: tr, type, ...range }) ? dispatch == null ? void 0 : dispatch(tr.removeMark(from, to, markType)) : dispatch == null ? void 0 : dispatch(tr.addMark(from, to, markType.create(attrs)));
|
671 | return true;
|
672 | }
|
673 | return convertCommand(originalToggleMark(markType, attrs))(props2);
|
674 | };
|
675 | }
|
676 | function markApplies(type, doc, ranges) {
|
677 | for (const { $from, $to } of ranges) {
|
678 | let markIsAllowed = $from.depth === 0 ? doc.type.allowsMarkType(type) : false;
|
679 | doc.nodesBetween($from.pos, $to.pos, (node) => {
|
680 | if (markIsAllowed) {
|
681 | return false;
|
682 | }
|
683 | markIsAllowed = node.inlineContent && node.type.allowsMarkType(type);
|
684 | return;
|
685 | });
|
686 | if (markIsAllowed) {
|
687 | return true;
|
688 | }
|
689 | }
|
690 | return false;
|
691 | }
|
692 | function applyMark(type, attrs, selectionPoint) {
|
693 | return ({ tr, dispatch, state }) => {
|
694 | const selection = getTextSelection(selectionPoint != null ? selectionPoint : tr.selection, tr.doc);
|
695 | const $cursor = getCursor(selection);
|
696 | const markType = isString(type) ? state.schema.marks[type] : type;
|
697 | invariant4(markType, {
|
698 | code: ErrorConstant4.SCHEMA,
|
699 | message: `Mark type: ${type} does not exist on the current schema.`
|
700 | });
|
701 | if (selection.empty && !$cursor || !markApplies(markType, tr.doc, selection.ranges)) {
|
702 | return false;
|
703 | }
|
704 | if (!dispatch) {
|
705 | return true;
|
706 | }
|
707 | if ($cursor) {
|
708 | tr.removeStoredMark(markType);
|
709 | if (attrs) {
|
710 | tr.addStoredMark(markType.create(attrs));
|
711 | }
|
712 | dispatch(tr);
|
713 | return true;
|
714 | }
|
715 | let containsMark = false;
|
716 | for (const { $from, $to } of selection.ranges) {
|
717 | if (containsMark) {
|
718 | break;
|
719 | }
|
720 | containsMark = tr.doc.rangeHasMark($from.pos, $to.pos, markType);
|
721 | }
|
722 | for (const { $from, $to } of selection.ranges) {
|
723 | if (containsMark) {
|
724 | tr.removeMark($from.pos, $to.pos, markType);
|
725 | }
|
726 | if (attrs) {
|
727 | tr.addMark($from.pos, $to.pos, markType.create(attrs));
|
728 | }
|
729 | }
|
730 | dispatch(tr);
|
731 | return true;
|
732 | };
|
733 | }
|
734 | function insertText(text, options = {}) {
|
735 | return ({ tr, dispatch, state }) => {
|
736 | const schema = state.schema;
|
737 | const selection = tr.selection;
|
738 | const { from = selection.from, to = from != null ? from : selection.to, marks = {} } = options;
|
739 | if (!dispatch) {
|
740 | return true;
|
741 | }
|
742 | tr.insertText(text, from, to);
|
743 | const end = assertGet2(tr.steps, tr.steps.length - 1).getMap().map(to);
|
744 | for (const [markName, attributes] of entries(marks)) {
|
745 | tr.addMark(from, end, assertGet2(schema.marks, markName).create(attributes));
|
746 | }
|
747 | dispatch(tr);
|
748 | return true;
|
749 | };
|
750 | }
|
751 |
|
752 |
|
753 | var CommandsExtension = class extends PlainExtension {
|
754 | constructor() {
|
755 | super(...arguments);
|
756 | __publicField(this, "_transaction");
|
757 | __publicField(this, "decorated", new Map());
|
758 | __publicField(this, "forceUpdateTransaction", (tr, ...keys3) => {
|
759 | const { forcedUpdates } = this.getCommandMeta(tr);
|
760 | this.setCommandMeta(tr, { forcedUpdates: uniqueArray([...forcedUpdates, ...keys3]) });
|
761 | return tr;
|
762 | });
|
763 | }
|
764 | get name() {
|
765 | return "commands";
|
766 | }
|
767 | get transaction() {
|
768 | const state = this.store.getState();
|
769 | if (!this._transaction) {
|
770 | this._transaction = state.tr;
|
771 | }
|
772 | const isValid = this._transaction.before.eq(state.doc);
|
773 | const hasSteps = !isEmptyArray2(this._transaction.steps);
|
774 | if (!isValid) {
|
775 | const tr = state.tr;
|
776 | if (hasSteps) {
|
777 | for (const step of this._transaction.steps) {
|
778 | tr.step(step);
|
779 | }
|
780 | }
|
781 | this._transaction = tr;
|
782 | }
|
783 | return this._transaction;
|
784 | }
|
785 | onCreate() {
|
786 | this.store.setStoreKey("getForcedUpdates", this.getForcedUpdates.bind(this));
|
787 | }
|
788 | onView(view) {
|
789 | var _a, _b, _c;
|
790 | const { extensions, helpers } = this.store;
|
791 | const commands = object4();
|
792 | const names = new Set();
|
793 | let allDecoratedCommands = object4();
|
794 | const chain = (tr) => {
|
795 | var _a2;
|
796 | const customChain = object4();
|
797 | const getTr = () => tr != null ? tr : this.transaction;
|
798 | let commandChain = [];
|
799 | const getChain = () => commandChain;
|
800 | for (const [name, command2] of Object.entries(commands)) {
|
801 | if ((_a2 = allDecoratedCommands[name]) == null ? void 0 : _a2.disableChaining) {
|
802 | continue;
|
803 | }
|
804 | customChain[name] = this.chainedFactory({
|
805 | chain: customChain,
|
806 | command: command2.original,
|
807 | getTr,
|
808 | getChain
|
809 | });
|
810 | }
|
811 | const dispatch = (transaction) => {
|
812 | invariant5(transaction === getTr(), {
|
813 | message: "Chaining currently only supports `CommandFunction` methods which do not use the `state.tr` property. Instead you should use the provided `tr` property."
|
814 | });
|
815 | };
|
816 | customChain.run = (options = {}) => {
|
817 | const commands2 = commandChain;
|
818 | commandChain = [];
|
819 | for (const cmd of commands2) {
|
820 | if (!cmd(dispatch) && options.exitEarly) {
|
821 | return;
|
822 | }
|
823 | }
|
824 | view.dispatch(getTr());
|
825 | };
|
826 | customChain.tr = () => {
|
827 | const commands2 = commandChain;
|
828 | commandChain = [];
|
829 | for (const cmd of commands2) {
|
830 | cmd(dispatch);
|
831 | }
|
832 | return getTr();
|
833 | };
|
834 | customChain.enabled = () => {
|
835 | for (const cmd of commandChain) {
|
836 | if (!cmd()) {
|
837 | return false;
|
838 | }
|
839 | }
|
840 | return true;
|
841 | };
|
842 | return customChain;
|
843 | };
|
844 | for (const extension2 of extensions) {
|
845 | const extensionCommands = (_b = (_a = extension2.createCommands) == null ? void 0 : _a.call(extension2)) != null ? _b : {};
|
846 | const decoratedCommands = (_c = extension2.decoratedCommands) != null ? _c : {};
|
847 | const active = {};
|
848 | allDecoratedCommands = { ...allDecoratedCommands, decoratedCommands };
|
849 | for (const [commandName, options] of Object.entries(decoratedCommands)) {
|
850 | const shortcut = isString2(options.shortcut) && options.shortcut.startsWith("_|") ? { shortcut: helpers.getNamedShortcut(options.shortcut, extension2.options) } : void 0;
|
851 | this.updateDecorated(commandName, { ...options, name: extension2.name, ...shortcut });
|
852 | extensionCommands[commandName] = extension2[commandName].bind(extension2);
|
853 | if (options.active) {
|
854 | active[commandName] = () => {
|
855 | var _a2, _b2;
|
856 | return (_b2 = (_a2 = options.active) == null ? void 0 : _a2.call(options, extension2.options, this.store)) != null ? _b2 : false;
|
857 | };
|
858 | }
|
859 | }
|
860 | if (isEmptyObject(extensionCommands)) {
|
861 | continue;
|
862 | }
|
863 | this.addCommands({ active, names, commands, extensionCommands });
|
864 | }
|
865 | const chainProperty = chain();
|
866 | for (const [key2, command2] of Object.entries(chainProperty)) {
|
867 | chain[key2] = command2;
|
868 | }
|
869 | this.store.setStoreKey("commands", commands);
|
870 | this.store.setStoreKey("chain", chain);
|
871 | this.store.setExtensionStore("commands", commands);
|
872 | this.store.setExtensionStore("chain", chain);
|
873 | }
|
874 | onStateUpdate({ state }) {
|
875 | this._transaction = state.tr;
|
876 | }
|
877 | createPlugin() {
|
878 | return {};
|
879 | }
|
880 | customDispatch(command2) {
|
881 | return command2;
|
882 | }
|
883 | insertText(text, options = {}) {
|
884 | if (isString2(text)) {
|
885 | return insertText(text, options);
|
886 | }
|
887 | return this.store.createPlaceholderCommand({
|
888 | promise: text,
|
889 | placeholder: { type: "inline" },
|
890 | onSuccess: (value, range, props) => {
|
891 | return this.insertText(value, { ...options, ...range })(props);
|
892 | }
|
893 | }).generateCommand();
|
894 | }
|
895 | selectText(selection, options = {}) {
|
896 | return ({ tr, dispatch }) => {
|
897 | const textSelection = getTextSelection2(selection, tr.doc);
|
898 | const selectionUnchanged = tr.selection.anchor === textSelection.anchor && tr.selection.head === textSelection.head;
|
899 | if (selectionUnchanged && !options.forceUpdate) {
|
900 | return false;
|
901 | }
|
902 | dispatch == null ? void 0 : dispatch(tr.setSelection(textSelection));
|
903 | return true;
|
904 | };
|
905 | }
|
906 | selectMark(type) {
|
907 | return (props) => {
|
908 | const { tr } = props;
|
909 | const range = getMarkRange(tr.selection.$from, type);
|
910 | if (!range) {
|
911 | return false;
|
912 | }
|
913 | return this.store.commands.selectText.original({ from: range.from, to: range.to })(props);
|
914 | };
|
915 | }
|
916 | delete(range) {
|
917 | return ({ tr, dispatch }) => {
|
918 | const { from, to } = range != null ? range : tr.selection;
|
919 | dispatch == null ? void 0 : dispatch(tr.delete(from, to));
|
920 | return true;
|
921 | };
|
922 | }
|
923 | emptyUpdate(action) {
|
924 | return ({ tr, dispatch }) => {
|
925 | if (dispatch) {
|
926 | action == null ? void 0 : action();
|
927 | dispatch(tr);
|
928 | }
|
929 | return true;
|
930 | };
|
931 | }
|
932 | forceUpdate(...keys3) {
|
933 | return ({ tr, dispatch }) => {
|
934 | dispatch == null ? void 0 : dispatch(this.forceUpdateTransaction(tr, ...keys3));
|
935 | return true;
|
936 | };
|
937 | }
|
938 | updateNodeAttributes(pos, attrs) {
|
939 | return ({ tr, dispatch }) => {
|
940 | dispatch == null ? void 0 : dispatch(tr.setNodeMarkup(pos, void 0, attrs));
|
941 | return true;
|
942 | };
|
943 | }
|
944 | setContent(content, selection) {
|
945 | return (props) => {
|
946 | const { tr, dispatch } = props;
|
947 | const state = this.store.manager.createState({ content, selection });
|
948 | if (!state) {
|
949 | return false;
|
950 | }
|
951 | dispatch == null ? void 0 : dispatch(tr.replaceRangeWith(0, tr.doc.nodeSize - 2, state.doc));
|
952 | return true;
|
953 | };
|
954 | }
|
955 | resetContent() {
|
956 | return (props) => {
|
957 | const { tr, dispatch } = props;
|
958 | const doc = this.store.manager.createEmptyDoc();
|
959 | if (doc) {
|
960 | return this.setContent(doc)(props);
|
961 | }
|
962 | dispatch == null ? void 0 : dispatch(tr.delete(0, tr.doc.nodeSize));
|
963 | return true;
|
964 | };
|
965 | }
|
966 | emptySelection() {
|
967 | return ({ tr, dispatch }) => {
|
968 | if (tr.selection.empty) {
|
969 | return false;
|
970 | }
|
971 | dispatch == null ? void 0 : dispatch(tr.setSelection(TextSelection.near(tr.selection.$anchor)));
|
972 | return true;
|
973 | };
|
974 | }
|
975 | insertNewLine() {
|
976 | return ({ dispatch, tr }) => {
|
977 | if (!isTextSelection(tr.selection)) {
|
978 | return false;
|
979 | }
|
980 | dispatch == null ? void 0 : dispatch(tr.insertText("\n"));
|
981 | return true;
|
982 | };
|
983 | }
|
984 | insertNode(node, options = {}) {
|
985 | return ({ dispatch, tr, state }) => {
|
986 | var _a, _b;
|
987 | const { attrs, range, selection, replaceEmptyParentBlock = false } = options;
|
988 | const { from, to, $from } = getTextSelection2((_a = selection != null ? selection : range) != null ? _a : tr.selection, tr.doc);
|
989 | if (isProsemirrorNode(node) || isProsemirrorFragment(node)) {
|
990 | const pos = $from.before($from.depth);
|
991 | dispatch == null ? void 0 : dispatch(replaceEmptyParentBlock && from === to && isEmptyBlockNode($from.parent) ? tr.replaceWith(pos, pos + $from.parent.nodeSize, node) : tr.replaceWith(from, to, node));
|
992 | return true;
|
993 | }
|
994 | const nodeType = isString2(node) ? state.schema.nodes[node] : node;
|
995 | invariant5(nodeType, {
|
996 | code: ErrorConstant5.SCHEMA,
|
997 | message: `The requested node type ${node} does not exist in the schema.`
|
998 | });
|
999 | const marks = (_b = options.marks) == null ? void 0 : _b.map((mark) => {
|
1000 | if (mark instanceof Mark) {
|
1001 | return mark;
|
1002 | }
|
1003 | const markType = isString2(mark) ? state.schema.marks[mark] : mark;
|
1004 | invariant5(markType, {
|
1005 | code: ErrorConstant5.SCHEMA,
|
1006 | message: `The requested mark type ${mark} does not exist in the schema.`
|
1007 | });
|
1008 | return markType.create();
|
1009 | });
|
1010 | const content = nodeType.createAndFill(attrs, isString2(options.content) ? state.schema.text(options.content) : options.content, marks);
|
1011 | if (!content) {
|
1012 | return false;
|
1013 | }
|
1014 | const isReplacement = from !== to;
|
1015 | dispatch == null ? void 0 : dispatch(isReplacement ? tr.replaceRangeWith(from, to, content) : tr.insert(from, content));
|
1016 | return true;
|
1017 | };
|
1018 | }
|
1019 | focus(position) {
|
1020 | return (props) => {
|
1021 | const { dispatch, tr } = props;
|
1022 | const { view } = this.store;
|
1023 | if (position === false) {
|
1024 | return false;
|
1025 | }
|
1026 | if (view.hasFocus() && (position === void 0 || position === true)) {
|
1027 | return false;
|
1028 | }
|
1029 | if (position === void 0 || position === true) {
|
1030 | const { from = 0, to = from } = tr.selection;
|
1031 | position = { from, to };
|
1032 | }
|
1033 | if (dispatch) {
|
1034 | this.delayedFocus();
|
1035 | }
|
1036 | return this.selectText(position)(props);
|
1037 | };
|
1038 | }
|
1039 | blur(position) {
|
1040 | return (props) => {
|
1041 | const { view } = this.store;
|
1042 | if (!view.hasFocus()) {
|
1043 | return false;
|
1044 | }
|
1045 | requestAnimationFrame(() => {
|
1046 | view.dom.blur();
|
1047 | });
|
1048 | return position ? this.selectText(position)(props) : true;
|
1049 | };
|
1050 | }
|
1051 | setBlockNodeType(nodeType, attrs, selection, preserveAttrs = true) {
|
1052 | return setBlockType(nodeType, attrs, selection, preserveAttrs);
|
1053 | }
|
1054 | toggleWrappingNode(nodeType, attrs, selection) {
|
1055 | return toggleWrap(nodeType, attrs, selection);
|
1056 | }
|
1057 | toggleBlockNodeItem(toggleProps) {
|
1058 | return toggleBlockItem(toggleProps);
|
1059 | }
|
1060 | wrapInNode(nodeType, attrs, range) {
|
1061 | return wrapIn(nodeType, attrs, range);
|
1062 | }
|
1063 | applyMark(markType, attrs, selection) {
|
1064 | return applyMark(markType, attrs, selection);
|
1065 | }
|
1066 | toggleMark(props) {
|
1067 | return toggleMark(props);
|
1068 | }
|
1069 | removeMark(props) {
|
1070 | return removeMark(props);
|
1071 | }
|
1072 | setMeta(name, value) {
|
1073 | return ({ tr }) => {
|
1074 | tr.setMeta(name, value);
|
1075 | return true;
|
1076 | };
|
1077 | }
|
1078 | selectAll() {
|
1079 | return this.selectText("all");
|
1080 | }
|
1081 | copy() {
|
1082 | return (props) => {
|
1083 | if (props.tr.selection.empty) {
|
1084 | return false;
|
1085 | }
|
1086 | if (props.dispatch) {
|
1087 | document.execCommand("copy");
|
1088 | }
|
1089 | return true;
|
1090 | };
|
1091 | }
|
1092 | paste() {
|
1093 | return this.store.createPlaceholderCommand({
|
1094 | promise: () => navigator.clipboard.readText(),
|
1095 | placeholder: { type: "inline" },
|
1096 | onSuccess: (value, selection, props) => {
|
1097 | return this.insertNode(htmlToProsemirrorNode({ content: value, schema: props.state.schema }), { selection })(props);
|
1098 | }
|
1099 | }).generateCommand();
|
1100 | }
|
1101 | cut() {
|
1102 | return (props) => {
|
1103 | if (props.tr.selection.empty) {
|
1104 | return false;
|
1105 | }
|
1106 | if (props.dispatch) {
|
1107 | document.execCommand("cut");
|
1108 | }
|
1109 | return true;
|
1110 | };
|
1111 | }
|
1112 | replaceText(props) {
|
1113 | return replaceText(props);
|
1114 | }
|
1115 | getAllCommandOptions() {
|
1116 | const uiCommands = {};
|
1117 | for (const [name, options] of this.decorated) {
|
1118 | if (isEmptyObject(options)) {
|
1119 | continue;
|
1120 | }
|
1121 | uiCommands[name] = options;
|
1122 | }
|
1123 | return uiCommands;
|
1124 | }
|
1125 | getCommandOptions(name) {
|
1126 | return this.decorated.get(name);
|
1127 | }
|
1128 | getCommandProp() {
|
1129 | return {
|
1130 | tr: this.transaction,
|
1131 | dispatch: this.store.view.dispatch,
|
1132 | state: this.store.view.state,
|
1133 | view: this.store.view
|
1134 | };
|
1135 | }
|
1136 | updateDecorated(name, options) {
|
1137 | var _a;
|
1138 | if (!options) {
|
1139 | this.decorated.delete(name);
|
1140 | return;
|
1141 | }
|
1142 | const decoratorOptions = (_a = this.decorated.get(name)) != null ? _a : { name: "" };
|
1143 | this.decorated.set(name, { ...decoratorOptions, ...options });
|
1144 | }
|
1145 | handleIosFocus() {
|
1146 | if (!environment2.isIos) {
|
1147 | return;
|
1148 | }
|
1149 | this.store.view.dom.focus();
|
1150 | }
|
1151 | delayedFocus() {
|
1152 | this.handleIosFocus();
|
1153 | requestAnimationFrame(() => {
|
1154 | this.store.view.focus();
|
1155 | this.store.view.dispatch(this.transaction.scrollIntoView());
|
1156 | });
|
1157 | }
|
1158 | getForcedUpdates(tr) {
|
1159 | return this.getCommandMeta(tr).forcedUpdates;
|
1160 | }
|
1161 | getCommandMeta(tr) {
|
1162 | var _a;
|
1163 | const meta = (_a = tr.getMeta(this.pluginKey)) != null ? _a : {};
|
1164 | return { ...DEFAULT_COMMAND_META, ...meta };
|
1165 | }
|
1166 | setCommandMeta(tr, update) {
|
1167 | const meta = this.getCommandMeta(tr);
|
1168 | tr.setMeta(this.pluginKey, { ...meta, ...update });
|
1169 | }
|
1170 | addCommands(props) {
|
1171 | const { extensionCommands, commands, names, active } = props;
|
1172 | for (const [name, command2] of entries2(extensionCommands)) {
|
1173 | throwIfNameNotUnique({ name, set: names, code: ErrorConstant5.DUPLICATE_COMMAND_NAMES });
|
1174 | invariant5(!forbiddenNames.has(name), {
|
1175 | code: ErrorConstant5.DUPLICATE_COMMAND_NAMES,
|
1176 | message: "The command name you chose is forbidden."
|
1177 | });
|
1178 | commands[name] = this.createUnchainedCommand(command2, active[name]);
|
1179 | }
|
1180 | }
|
1181 | unchainedFactory(props) {
|
1182 | return (...args) => {
|
1183 | const { shouldDispatch = true, command: command2 } = props;
|
1184 | const { view } = this.store;
|
1185 | const { state } = view;
|
1186 | let dispatch;
|
1187 | if (shouldDispatch) {
|
1188 | dispatch = view.dispatch;
|
1189 | }
|
1190 | return command2(...args)({ state, dispatch, view, tr: state.tr });
|
1191 | };
|
1192 | }
|
1193 | createUnchainedCommand(command2, active) {
|
1194 | const unchainedCommand = this.unchainedFactory({ command: command2 });
|
1195 | unchainedCommand.enabled = this.unchainedFactory({ command: command2, shouldDispatch: false });
|
1196 | unchainedCommand.isEnabled = unchainedCommand.enabled;
|
1197 | unchainedCommand.original = command2;
|
1198 | unchainedCommand.active = active;
|
1199 | return unchainedCommand;
|
1200 | }
|
1201 | chainedFactory(props) {
|
1202 | return (...args) => {
|
1203 | const { chain: chained, command: command2, getTr, getChain } = props;
|
1204 | const lazyChain = getChain();
|
1205 | const { view } = this.store;
|
1206 | const { state } = view;
|
1207 | lazyChain.push((dispatch) => {
|
1208 | return command2(...args)({ state, dispatch, view, tr: getTr() });
|
1209 | });
|
1210 | return chained;
|
1211 | };
|
1212 | }
|
1213 | };
|
1214 | __decorateClass([
|
1215 | command()
|
1216 | ], CommandsExtension.prototype, "customDispatch", 1);
|
1217 | __decorateClass([
|
1218 | command()
|
1219 | ], CommandsExtension.prototype, "insertText", 1);
|
1220 | __decorateClass([
|
1221 | command()
|
1222 | ], CommandsExtension.prototype, "selectText", 1);
|
1223 | __decorateClass([
|
1224 | command()
|
1225 | ], CommandsExtension.prototype, "selectMark", 1);
|
1226 | __decorateClass([
|
1227 | command()
|
1228 | ], CommandsExtension.prototype, "delete", 1);
|
1229 | __decorateClass([
|
1230 | command()
|
1231 | ], CommandsExtension.prototype, "emptyUpdate", 1);
|
1232 | __decorateClass([
|
1233 | command()
|
1234 | ], CommandsExtension.prototype, "forceUpdate", 1);
|
1235 | __decorateClass([
|
1236 | command()
|
1237 | ], CommandsExtension.prototype, "updateNodeAttributes", 1);
|
1238 | __decorateClass([
|
1239 | command()
|
1240 | ], CommandsExtension.prototype, "setContent", 1);
|
1241 | __decorateClass([
|
1242 | command()
|
1243 | ], CommandsExtension.prototype, "resetContent", 1);
|
1244 | __decorateClass([
|
1245 | command()
|
1246 | ], CommandsExtension.prototype, "emptySelection", 1);
|
1247 | __decorateClass([
|
1248 | command()
|
1249 | ], CommandsExtension.prototype, "insertNewLine", 1);
|
1250 | __decorateClass([
|
1251 | command()
|
1252 | ], CommandsExtension.prototype, "insertNode", 1);
|
1253 | __decorateClass([
|
1254 | command()
|
1255 | ], CommandsExtension.prototype, "focus", 1);
|
1256 | __decorateClass([
|
1257 | command()
|
1258 | ], CommandsExtension.prototype, "blur", 1);
|
1259 | __decorateClass([
|
1260 | command()
|
1261 | ], CommandsExtension.prototype, "setBlockNodeType", 1);
|
1262 | __decorateClass([
|
1263 | command()
|
1264 | ], CommandsExtension.prototype, "toggleWrappingNode", 1);
|
1265 | __decorateClass([
|
1266 | command()
|
1267 | ], CommandsExtension.prototype, "toggleBlockNodeItem", 1);
|
1268 | __decorateClass([
|
1269 | command()
|
1270 | ], CommandsExtension.prototype, "wrapInNode", 1);
|
1271 | __decorateClass([
|
1272 | command()
|
1273 | ], CommandsExtension.prototype, "applyMark", 1);
|
1274 | __decorateClass([
|
1275 | command()
|
1276 | ], CommandsExtension.prototype, "toggleMark", 1);
|
1277 | __decorateClass([
|
1278 | command()
|
1279 | ], CommandsExtension.prototype, "removeMark", 1);
|
1280 | __decorateClass([
|
1281 | command()
|
1282 | ], CommandsExtension.prototype, "setMeta", 1);
|
1283 | __decorateClass([
|
1284 | command({
|
1285 | description: ({ t }) => t(Messages.SELECT_ALL_DESCRIPTION),
|
1286 | label: ({ t }) => t(Messages.SELECT_ALL_LABEL),
|
1287 | shortcut: NamedShortcut.SelectAll
|
1288 | })
|
1289 | ], CommandsExtension.prototype, "selectAll", 1);
|
1290 | __decorateClass([
|
1291 | command({
|
1292 | description: ({ t }) => t(Messages.COPY_DESCRIPTION),
|
1293 | label: ({ t }) => t(Messages.COPY_LABEL),
|
1294 | shortcut: NamedShortcut.Copy,
|
1295 | icon: "fileCopyLine"
|
1296 | })
|
1297 | ], CommandsExtension.prototype, "copy", 1);
|
1298 | __decorateClass([
|
1299 | command({
|
1300 | description: ({ t }) => t(Messages.PASTE_DESCRIPTION),
|
1301 | label: ({ t }) => t(Messages.PASTE_LABEL),
|
1302 | shortcut: NamedShortcut.Paste,
|
1303 | icon: "clipboardLine"
|
1304 | })
|
1305 | ], CommandsExtension.prototype, "paste", 1);
|
1306 | __decorateClass([
|
1307 | command({
|
1308 | description: ({ t }) => t(Messages.CUT_DESCRIPTION),
|
1309 | label: ({ t }) => t(Messages.CUT_LABEL),
|
1310 | shortcut: NamedShortcut.Cut,
|
1311 | icon: "scissorsFill"
|
1312 | })
|
1313 | ], CommandsExtension.prototype, "cut", 1);
|
1314 | __decorateClass([
|
1315 | command()
|
1316 | ], CommandsExtension.prototype, "replaceText", 1);
|
1317 | __decorateClass([
|
1318 | helper()
|
1319 | ], CommandsExtension.prototype, "getAllCommandOptions", 1);
|
1320 | __decorateClass([
|
1321 | helper()
|
1322 | ], CommandsExtension.prototype, "getCommandOptions", 1);
|
1323 | __decorateClass([
|
1324 | helper()
|
1325 | ], CommandsExtension.prototype, "getCommandProp", 1);
|
1326 | CommandsExtension = __decorateClass([
|
1327 | extension({
|
1328 | defaultPriority: ExtensionPriority3.Highest,
|
1329 | defaultOptions: { trackerClassName: "remirror-tracker-position", trackerNodeName: "span" },
|
1330 | staticKeys: ["trackerClassName", "trackerNodeName"]
|
1331 | })
|
1332 | ], CommandsExtension);
|
1333 | var DEFAULT_COMMAND_META = {
|
1334 | forcedUpdates: []
|
1335 | };
|
1336 | var forbiddenNames = new Set(["run", "chain", "original", "raw", "enabled", "tr"]);
|
1337 |
|
1338 |
|
1339 | import { ExtensionPriority as ExtensionPriority4 } from "@remirror/core-constants";
|
1340 | import { isNumber, isString as isString3, uniqueArray as uniqueArray2, uniqueId } from "@remirror/core-helpers";
|
1341 | import { findNodeAtPosition, isNodeSelection } from "@remirror/core-utils";
|
1342 | import { Decoration, DecorationSet } from "@remirror/pm/view";
|
1343 | var DecorationsExtension = class extends PlainExtension {
|
1344 | constructor() {
|
1345 | super(...arguments);
|
1346 | __publicField(this, "placeholders", DecorationSet.empty);
|
1347 | __publicField(this, "placeholderWidgets", new Map());
|
1348 | __publicField(this, "createPlaceholderCommand", (props) => {
|
1349 | const id = uniqueId();
|
1350 | const { promise, placeholder, onFailure, onSuccess } = props;
|
1351 | return new DelayedCommand(promise).validate((props2) => {
|
1352 | return this.addPlaceholder(id, placeholder)(props2);
|
1353 | }).success((props2) => {
|
1354 | var _a;
|
1355 | const { state, tr, dispatch, view, value } = props2;
|
1356 | const range = this.store.helpers.findPlaceholder(id);
|
1357 | if (!range) {
|
1358 | const error = new Error("The placeholder has been removed");
|
1359 | return (_a = onFailure == null ? void 0 : onFailure({ error, state, tr, dispatch, view })) != null ? _a : false;
|
1360 | }
|
1361 | this.removePlaceholder(id)({ state, tr, view, dispatch: () => {
|
1362 | } });
|
1363 | return onSuccess(value, range, { state, tr, dispatch, view });
|
1364 | }).failure((props2) => {
|
1365 | var _a;
|
1366 | this.removePlaceholder(id)({ ...props2, dispatch: () => {
|
1367 | } });
|
1368 | return (_a = onFailure == null ? void 0 : onFailure(props2)) != null ? _a : false;
|
1369 | });
|
1370 | });
|
1371 | }
|
1372 | get name() {
|
1373 | return "decorations";
|
1374 | }
|
1375 | onCreate() {
|
1376 | this.store.setExtensionStore("createPlaceholderCommand", this.createPlaceholderCommand);
|
1377 | }
|
1378 | createPlugin() {
|
1379 | return {
|
1380 | state: {
|
1381 | init: () => {
|
1382 | },
|
1383 | apply: (tr) => {
|
1384 | var _a, _b, _c, _d, _e, _f;
|
1385 | const { added, clearTrackers, removed, updated } = this.getMeta(tr);
|
1386 | if (clearTrackers) {
|
1387 | this.placeholders = DecorationSet.empty;
|
1388 | for (const [, widget] of this.placeholderWidgets) {
|
1389 | (_b = (_a = widget.spec).onDestroy) == null ? void 0 : _b.call(_a, this.store.view, widget.spec.element);
|
1390 | }
|
1391 | this.placeholderWidgets.clear();
|
1392 | return;
|
1393 | }
|
1394 | this.placeholders = this.placeholders.map(tr.mapping, tr.doc, {
|
1395 | onRemove: (spec) => {
|
1396 | var _a2, _b2;
|
1397 | const widget = this.placeholderWidgets.get(spec.id);
|
1398 | if (widget) {
|
1399 | (_b2 = (_a2 = widget.spec).onDestroy) == null ? void 0 : _b2.call(_a2, this.store.view, widget.spec.element);
|
1400 | }
|
1401 | }
|
1402 | });
|
1403 | for (const [, widget] of this.placeholderWidgets) {
|
1404 | (_d = (_c = widget.spec).onUpdate) == null ? void 0 : _d.call(_c, this.store.view, widget.from, widget.spec.element, widget.spec.data);
|
1405 | }
|
1406 | for (const placeholder of added) {
|
1407 | if (placeholder.type === "inline") {
|
1408 | this.addInlinePlaceholder(placeholder, tr);
|
1409 | continue;
|
1410 | }
|
1411 | if (placeholder.type === "node") {
|
1412 | this.addNodePlaceholder(placeholder, tr);
|
1413 | continue;
|
1414 | }
|
1415 | if (placeholder.type === "widget") {
|
1416 | this.addWidgetPlaceholder(placeholder, tr);
|
1417 | continue;
|
1418 | }
|
1419 | }
|
1420 | for (const { id, data } of updated) {
|
1421 | const widget = this.placeholderWidgets.get(id);
|
1422 | if (!widget) {
|
1423 | continue;
|
1424 | }
|
1425 | const updatedWidget = Decoration.widget(widget.from, widget.spec.element, {
|
1426 | ...widget.spec,
|
1427 | data
|
1428 | });
|
1429 | this.placeholders = this.placeholders.remove([widget]).add(tr.doc, [updatedWidget]);
|
1430 | this.placeholderWidgets.set(id, updatedWidget);
|
1431 | }
|
1432 | for (const id of removed) {
|
1433 | const found = this.placeholders.find(void 0, void 0, (spec) => spec.id === id && spec.__type === __type);
|
1434 | const widget = this.placeholderWidgets.get(id);
|
1435 | if (widget) {
|
1436 | (_f = (_e = widget.spec).onDestroy) == null ? void 0 : _f.call(_e, this.store.view, widget.spec.element);
|
1437 | }
|
1438 | this.placeholders = this.placeholders.remove(found);
|
1439 | this.placeholderWidgets.delete(id);
|
1440 | }
|
1441 | }
|
1442 | },
|
1443 | props: {
|
1444 | decorations: (state) => {
|
1445 | let decorationSet = this.options.decorations(state);
|
1446 | decorationSet = decorationSet.add(state.doc, this.placeholders.find());
|
1447 | for (const extension2 of this.store.extensions) {
|
1448 | if (!extension2.createDecorations) {
|
1449 | continue;
|
1450 | }
|
1451 | const decorations = extension2.createDecorations(state).find();
|
1452 | decorationSet = decorationSet.add(state.doc, decorations);
|
1453 | }
|
1454 | return decorationSet;
|
1455 | },
|
1456 | handleDOMEvents: {
|
1457 | blur: (view) => {
|
1458 | if (this.options.persistentSelectionClass) {
|
1459 | view.dispatch(view.state.tr.setMeta(persistentSelectionFocusKey, false));
|
1460 | }
|
1461 | return false;
|
1462 | },
|
1463 | focus: (view) => {
|
1464 | if (this.options.persistentSelectionClass) {
|
1465 | view.dispatch(view.state.tr.setMeta(persistentSelectionFocusKey, true));
|
1466 | }
|
1467 | return false;
|
1468 | }
|
1469 | }
|
1470 | }
|
1471 | };
|
1472 | }
|
1473 | updateDecorations() {
|
1474 | return ({ tr, dispatch }) => (dispatch == null ? void 0 : dispatch(tr), true);
|
1475 | }
|
1476 | addPlaceholder(id, placeholder, deleteSelection) {
|
1477 | return ({ dispatch, tr }) => {
|
1478 | return this.addPlaceholderTransaction(id, placeholder, tr, !dispatch) ? (dispatch == null ? void 0 : dispatch(deleteSelection ? tr.deleteSelection() : tr), true) : false;
|
1479 | };
|
1480 | }
|
1481 | updatePlaceholder(id, data) {
|
1482 | return ({ dispatch, tr }) => {
|
1483 | return this.updatePlaceholderTransaction({ id, data, tr, checkOnly: !dispatch }) ? (dispatch == null ? void 0 : dispatch(tr), true) : false;
|
1484 | };
|
1485 | }
|
1486 | removePlaceholder(id) {
|
1487 | return ({ dispatch, tr }) => {
|
1488 | return this.removePlaceholderTransaction({ id, tr, checkOnly: !dispatch }) ? (dispatch == null ? void 0 : dispatch(tr), true) : false;
|
1489 | };
|
1490 | }
|
1491 | clearPlaceholders() {
|
1492 | return ({ tr, dispatch }) => {
|
1493 | return this.clearPlaceholdersTransaction({ tr, checkOnly: !dispatch }) ? (dispatch == null ? void 0 : dispatch(tr), true) : false;
|
1494 | };
|
1495 | }
|
1496 | findPlaceholder(id) {
|
1497 | return this.findAllPlaceholders().get(id);
|
1498 | }
|
1499 | findAllPlaceholders() {
|
1500 | const trackers = new Map();
|
1501 | const found = this.placeholders.find(void 0, void 0, (spec) => spec.__type === __type);
|
1502 | for (const decoration of found) {
|
1503 | trackers.set(decoration.spec.id, { from: decoration.from, to: decoration.to });
|
1504 | }
|
1505 | return trackers;
|
1506 | }
|
1507 | createDecorations(state) {
|
1508 | var _a, _b, _c;
|
1509 | const { persistentSelectionClass } = this.options;
|
1510 | if (!persistentSelectionClass || ((_a = this.store.view) == null ? void 0 : _a.hasFocus()) || ((_c = (_b = this.store.helpers).isInteracting) == null ? void 0 : _c.call(_b))) {
|
1511 | return DecorationSet.empty;
|
1512 | }
|
1513 | return generatePersistentSelectionDecorations(state, DecorationSet.empty, {
|
1514 | class: isString3(persistentSelectionClass) ? persistentSelectionClass : "selection"
|
1515 | });
|
1516 | }
|
1517 | onApplyState() {
|
1518 | }
|
1519 | addWidgetPlaceholder(placeholder, tr) {
|
1520 | var _a;
|
1521 | const { pos, createElement, onDestroy, onUpdate, className, nodeName, id, type } = placeholder;
|
1522 | const element = (_a = createElement == null ? void 0 : createElement(this.store.view, pos)) != null ? _a : document.createElement(nodeName);
|
1523 | element.classList.add(className);
|
1524 | const decoration = Decoration.widget(pos, element, {
|
1525 | id,
|
1526 | __type,
|
1527 | type,
|
1528 | element,
|
1529 | onDestroy,
|
1530 | onUpdate
|
1531 | });
|
1532 | this.placeholderWidgets.set(id, decoration);
|
1533 | this.placeholders = this.placeholders.add(tr.doc, [decoration]);
|
1534 | }
|
1535 | addInlinePlaceholder(placeholder, tr) {
|
1536 | const {
|
1537 | from = tr.selection.from,
|
1538 | to = tr.selection.to,
|
1539 | className,
|
1540 | nodeName,
|
1541 | id,
|
1542 | type
|
1543 | } = placeholder;
|
1544 | let decoration;
|
1545 | if (from === to) {
|
1546 | const element = document.createElement(nodeName);
|
1547 | element.classList.add(className);
|
1548 | decoration = Decoration.widget(from, element, {
|
1549 | id,
|
1550 | type,
|
1551 | __type,
|
1552 | widget: element
|
1553 | });
|
1554 | } else {
|
1555 | decoration = Decoration.inline(from, to, { nodeName, class: className }, {
|
1556 | id,
|
1557 | __type
|
1558 | });
|
1559 | }
|
1560 | this.placeholders = this.placeholders.add(tr.doc, [decoration]);
|
1561 | }
|
1562 | addNodePlaceholder(placeholder, tr) {
|
1563 | const { pos, className, nodeName, id } = placeholder;
|
1564 | const $pos = isNumber(pos) ? tr.doc.resolve(pos) : tr.selection.$from;
|
1565 | const found = isNumber(pos) ? $pos.nodeAfter ? { pos, end: $pos.nodeAfter.nodeSize } : void 0 : findNodeAtPosition($pos);
|
1566 | if (!found) {
|
1567 | return;
|
1568 | }
|
1569 | const decoration = Decoration.node(found.pos, found.end, { nodeName, class: className }, { id, __type });
|
1570 | this.placeholders = this.placeholders.add(tr.doc, [decoration]);
|
1571 | }
|
1572 | withRequiredBase(id, placeholder) {
|
1573 | const { placeholderNodeName, placeholderClassName } = this.options;
|
1574 | const { nodeName = placeholderNodeName, className, ...rest } = placeholder;
|
1575 | const classes = (className ? [placeholderClassName, className] : [placeholderClassName]).join(" ");
|
1576 | return { nodeName, className: classes, ...rest, id };
|
1577 | }
|
1578 | getMeta(tr) {
|
1579 | var _a;
|
1580 | const meta = (_a = tr.getMeta(this.pluginKey)) != null ? _a : {};
|
1581 | return { ...DEFAULT_PLACEHOLDER_META, ...meta };
|
1582 | }
|
1583 | setMeta(tr, update) {
|
1584 | const meta = this.getMeta(tr);
|
1585 | tr.setMeta(this.pluginKey, { ...meta, ...update });
|
1586 | }
|
1587 | addPlaceholderTransaction(id, placeholder, tr, checkOnly = false) {
|
1588 | const existingPosition = this.findPlaceholder(id);
|
1589 | if (existingPosition) {
|
1590 | return false;
|
1591 | }
|
1592 | if (checkOnly) {
|
1593 | return true;
|
1594 | }
|
1595 | const { added } = this.getMeta(tr);
|
1596 | this.setMeta(tr, {
|
1597 | added: [...added, this.withRequiredBase(id, placeholder)]
|
1598 | });
|
1599 | return true;
|
1600 | }
|
1601 | updatePlaceholderTransaction(props) {
|
1602 | const { id, tr, checkOnly = false, data } = props;
|
1603 | const existingPosition = this.findPlaceholder(id);
|
1604 | if (!existingPosition) {
|
1605 | return false;
|
1606 | }
|
1607 | if (checkOnly) {
|
1608 | return true;
|
1609 | }
|
1610 | const { updated } = this.getMeta(tr);
|
1611 | this.setMeta(tr, { updated: uniqueArray2([...updated, { id, data }]) });
|
1612 | return true;
|
1613 | }
|
1614 | removePlaceholderTransaction(props) {
|
1615 | const { id, tr, checkOnly = false } = props;
|
1616 | const existingPosition = this.findPlaceholder(id);
|
1617 | if (!existingPosition) {
|
1618 | return false;
|
1619 | }
|
1620 | if (checkOnly) {
|
1621 | return true;
|
1622 | }
|
1623 | const { removed } = this.getMeta(tr);
|
1624 | this.setMeta(tr, { removed: uniqueArray2([...removed, id]) });
|
1625 | return true;
|
1626 | }
|
1627 | clearPlaceholdersTransaction(props) {
|
1628 | const { tr, checkOnly = false } = props;
|
1629 | const positionTrackerState = this.getPluginState();
|
1630 | if (positionTrackerState === DecorationSet.empty) {
|
1631 | return false;
|
1632 | }
|
1633 | if (checkOnly) {
|
1634 | return true;
|
1635 | }
|
1636 | this.setMeta(tr, { clearTrackers: true });
|
1637 | return true;
|
1638 | }
|
1639 | };
|
1640 | __decorateClass([
|
1641 | command()
|
1642 | ], DecorationsExtension.prototype, "updateDecorations", 1);
|
1643 | __decorateClass([
|
1644 | command()
|
1645 | ], DecorationsExtension.prototype, "addPlaceholder", 1);
|
1646 | __decorateClass([
|
1647 | command()
|
1648 | ], DecorationsExtension.prototype, "updatePlaceholder", 1);
|
1649 | __decorateClass([
|
1650 | command()
|
1651 | ], DecorationsExtension.prototype, "removePlaceholder", 1);
|
1652 | __decorateClass([
|
1653 | command()
|
1654 | ], DecorationsExtension.prototype, "clearPlaceholders", 1);
|
1655 | __decorateClass([
|
1656 | helper()
|
1657 | ], DecorationsExtension.prototype, "findPlaceholder", 1);
|
1658 | __decorateClass([
|
1659 | helper()
|
1660 | ], DecorationsExtension.prototype, "findAllPlaceholders", 1);
|
1661 | DecorationsExtension = __decorateClass([
|
1662 | extension({
|
1663 | defaultOptions: {
|
1664 | persistentSelectionClass: void 0,
|
1665 | placeholderClassName: "placeholder",
|
1666 | placeholderNodeName: "span"
|
1667 | },
|
1668 | staticKeys: ["placeholderClassName", "placeholderNodeName"],
|
1669 | handlerKeys: ["decorations"],
|
1670 | handlerKeyOptions: {
|
1671 | decorations: {
|
1672 | reducer: {
|
1673 | accumulator: (accumulated, latestValue, state) => {
|
1674 | return accumulated.add(state.doc, latestValue.find());
|
1675 | },
|
1676 | getDefault: () => DecorationSet.empty
|
1677 | }
|
1678 | }
|
1679 | },
|
1680 | defaultPriority: ExtensionPriority4.Low
|
1681 | })
|
1682 | ], DecorationsExtension);
|
1683 | var DEFAULT_PLACEHOLDER_META = {
|
1684 | added: [],
|
1685 | updated: [],
|
1686 | clearTrackers: false,
|
1687 | removed: []
|
1688 | };
|
1689 | var __type = "placeholderDecoration";
|
1690 | var persistentSelectionFocusKey = "persistentSelectionFocus";
|
1691 | function generatePersistentSelectionDecorations(state, decorationSet, attrs) {
|
1692 | const { selection, doc } = state;
|
1693 | if (selection.empty) {
|
1694 | return decorationSet;
|
1695 | }
|
1696 | const { from, to } = selection;
|
1697 | const decoration = isNodeSelection(selection) ? Decoration.node(from, to, attrs) : Decoration.inline(from, to, attrs);
|
1698 | return decorationSet.add(doc, [decoration]);
|
1699 | }
|
1700 |
|
1701 |
|
1702 | import { ExtensionPriority as ExtensionPriority5 } from "@remirror/core-constants";
|
1703 | var DocChangedExtension = class extends PlainExtension {
|
1704 | get name() {
|
1705 | return "docChanged";
|
1706 | }
|
1707 | onStateUpdate(props) {
|
1708 | const { firstUpdate, transactions, tr } = props;
|
1709 | if (firstUpdate) {
|
1710 | return;
|
1711 | }
|
1712 | if ((transactions != null ? transactions : [tr]).some((tr2) => tr2 == null ? void 0 : tr2.docChanged)) {
|
1713 | this.options.docChanged(props);
|
1714 | }
|
1715 | }
|
1716 | };
|
1717 | DocChangedExtension = __decorateClass([
|
1718 | extension({
|
1719 | handlerKeys: ["docChanged"],
|
1720 | handlerKeyOptions: {
|
1721 | docChanged: { earlyReturnValue: false }
|
1722 | },
|
1723 | defaultPriority: ExtensionPriority5.Lowest
|
1724 | })
|
1725 | ], DocChangedExtension);
|
1726 |
|
1727 |
|
1728 | import { ErrorConstant as ErrorConstant6, NULL_CHARACTER } from "@remirror/core-constants";
|
1729 | import { entries as entries3, isEmptyObject as isEmptyObject2, object as object5 } from "@remirror/core-helpers";
|
1730 | import {
|
1731 | containsAttributes,
|
1732 | getActiveNode,
|
1733 | getMarkRange as getMarkRange2,
|
1734 | htmlToProsemirrorNode as htmlToProsemirrorNode2,
|
1735 | isMarkActive as isMarkActive2,
|
1736 | isNodeActive,
|
1737 | isSelectionEmpty,
|
1738 | prosemirrorNodeToHtml
|
1739 | } from "@remirror/core-utils";
|
1740 | var HelpersExtension = class extends PlainExtension {
|
1741 | get name() {
|
1742 | return "helpers";
|
1743 | }
|
1744 | onCreate() {
|
1745 | var _a, _b, _c;
|
1746 | this.store.setStringHandler("text", this.textToProsemirrorNode.bind(this));
|
1747 | this.store.setStringHandler("html", htmlToProsemirrorNode2);
|
1748 | const helpers = object5();
|
1749 | const active = object5();
|
1750 | const attrs = object5();
|
1751 | const names = new Set();
|
1752 | for (const extension2 of this.store.extensions) {
|
1753 | if (isNodeExtension(extension2)) {
|
1754 | active[extension2.name] = (attrs2) => {
|
1755 | return isNodeActive({ state: this.store.getState(), type: extension2.type, attrs: attrs2 });
|
1756 | };
|
1757 | attrs[extension2.name] = (attrs2) => {
|
1758 | var _a2;
|
1759 | return (_a2 = getActiveNode({ state: this.store.getState(), type: extension2.type, attrs: attrs2 })) == null ? void 0 : _a2.node.attrs;
|
1760 | };
|
1761 | }
|
1762 | if (isMarkExtension(extension2)) {
|
1763 | active[extension2.name] = (attrs2) => {
|
1764 | return isMarkActive2({ trState: this.store.getState(), type: extension2.type, attrs: attrs2 });
|
1765 | };
|
1766 | attrs[extension2.name] = (attrs2) => {
|
1767 | const markRange = getMarkRange2(this.store.getState().selection.$from, extension2.type);
|
1768 | if (!markRange || !attrs2) {
|
1769 | return markRange == null ? void 0 : markRange.mark.attrs;
|
1770 | }
|
1771 | if (containsAttributes(markRange.mark, attrs2)) {
|
1772 | return markRange.mark.attrs;
|
1773 | }
|
1774 | return;
|
1775 | };
|
1776 | }
|
1777 | const extensionHelpers = (_b = (_a = extension2.createHelpers) == null ? void 0 : _a.call(extension2)) != null ? _b : {};
|
1778 | for (const helperName of Object.keys((_c = extension2.decoratedHelpers) != null ? _c : {})) {
|
1779 | extensionHelpers[helperName] = extension2[helperName].bind(extension2);
|
1780 | }
|
1781 | if (isEmptyObject2(extensionHelpers)) {
|
1782 | continue;
|
1783 | }
|
1784 | for (const [name, helper2] of entries3(extensionHelpers)) {
|
1785 | throwIfNameNotUnique({ name, set: names, code: ErrorConstant6.DUPLICATE_HELPER_NAMES });
|
1786 | helpers[name] = helper2;
|
1787 | }
|
1788 | }
|
1789 | this.store.setStoreKey("attrs", attrs);
|
1790 | this.store.setStoreKey("active", active);
|
1791 | this.store.setStoreKey("helpers", helpers);
|
1792 | this.store.setExtensionStore("attrs", attrs);
|
1793 | this.store.setExtensionStore("active", active);
|
1794 | this.store.setExtensionStore("helpers", helpers);
|
1795 | }
|
1796 | isSelectionEmpty(state = this.store.getState()) {
|
1797 | return isSelectionEmpty(state);
|
1798 | }
|
1799 | isViewEditable(state = this.store.getState()) {
|
1800 | var _a, _b, _c;
|
1801 | return (_c = (_b = (_a = this.store.view.props).editable) == null ? void 0 : _b.call(_a, state)) != null ? _c : false;
|
1802 | }
|
1803 | getStateJSON(state = this.store.getState()) {
|
1804 | return state.toJSON();
|
1805 | }
|
1806 | getJSON(state = this.store.getState()) {
|
1807 | return state.doc.toJSON();
|
1808 | }
|
1809 | getRemirrorJSON(state = this.store.getState()) {
|
1810 | return this.getJSON(state);
|
1811 | }
|
1812 | insertHtml(html, options) {
|
1813 | return (props) => {
|
1814 | const { state } = props;
|
1815 | const fragment = htmlToProsemirrorNode2({
|
1816 | content: html,
|
1817 | schema: state.schema,
|
1818 | fragment: true
|
1819 | });
|
1820 | return this.store.commands.insertNode.original(fragment, options)(props);
|
1821 | };
|
1822 | }
|
1823 | getText({
|
1824 | lineBreakDivider = "\n\n",
|
1825 | state = this.store.getState()
|
1826 | } = {}) {
|
1827 | return state.doc.textBetween(0, state.doc.content.size, lineBreakDivider, NULL_CHARACTER);
|
1828 | }
|
1829 | getTextBetween(from, to, doc = this.store.getState().doc) {
|
1830 | return doc.textBetween(from, to, "\n\n", NULL_CHARACTER);
|
1831 | }
|
1832 | getHTML(state = this.store.getState()) {
|
1833 | return prosemirrorNodeToHtml(state.doc, this.store.document);
|
1834 | }
|
1835 | textToProsemirrorNode(options) {
|
1836 | const content = `<pre>${options.content}</pre>`;
|
1837 | return this.store.stringHandlers.html({ ...options, content });
|
1838 | }
|
1839 | };
|
1840 | __decorateClass([
|
1841 | helper()
|
1842 | ], HelpersExtension.prototype, "isSelectionEmpty", 1);
|
1843 | __decorateClass([
|
1844 | helper()
|
1845 | ], HelpersExtension.prototype, "isViewEditable", 1);
|
1846 | __decorateClass([
|
1847 | helper()
|
1848 | ], HelpersExtension.prototype, "getStateJSON", 1);
|
1849 | __decorateClass([
|
1850 | helper()
|
1851 | ], HelpersExtension.prototype, "getJSON", 1);
|
1852 | __decorateClass([
|
1853 | helper()
|
1854 | ], HelpersExtension.prototype, "getRemirrorJSON", 1);
|
1855 | __decorateClass([
|
1856 | command()
|
1857 | ], HelpersExtension.prototype, "insertHtml", 1);
|
1858 | __decorateClass([
|
1859 | helper()
|
1860 | ], HelpersExtension.prototype, "getText", 1);
|
1861 | __decorateClass([
|
1862 | helper()
|
1863 | ], HelpersExtension.prototype, "getTextBetween", 1);
|
1864 | __decorateClass([
|
1865 | helper()
|
1866 | ], HelpersExtension.prototype, "getHTML", 1);
|
1867 | HelpersExtension = __decorateClass([
|
1868 | extension({})
|
1869 | ], HelpersExtension);
|
1870 |
|
1871 |
|
1872 | import { ExtensionPriority as ExtensionPriority6, ExtensionTag } from "@remirror/core-constants";
|
1873 | import { inputRules } from "@remirror/pm/inputrules";
|
1874 | var InputRulesExtension = class extends PlainExtension {
|
1875 | get name() {
|
1876 | return "inputRules";
|
1877 | }
|
1878 | onCreate() {
|
1879 | this.store.setExtensionStore("rebuildInputRules", this.rebuildInputRules.bind(this));
|
1880 | }
|
1881 | createExternalPlugins() {
|
1882 | return [this.generateInputRulesPlugin()];
|
1883 | }
|
1884 | generateInputRulesPlugin() {
|
1885 | var _a, _b;
|
1886 | const rules = [];
|
1887 | const invalidMarks = this.store.markTags[ExtensionTag.ExcludeInputRules];
|
1888 | for (const extension2 of this.store.extensions) {
|
1889 | if (((_a = this.store.managerSettings.exclude) == null ? void 0 : _a.inputRules) || !extension2.createInputRules || ((_b = extension2.options.exclude) == null ? void 0 : _b.inputRules)) {
|
1890 | continue;
|
1891 | }
|
1892 | for (const rule of extension2.createInputRules()) {
|
1893 | rule.shouldSkip = this.options.shouldSkipInputRule;
|
1894 | rule.invalidMarks = invalidMarks;
|
1895 | rules.push(rule);
|
1896 | }
|
1897 | }
|
1898 | return inputRules({ rules });
|
1899 | }
|
1900 | rebuildInputRules() {
|
1901 | this.store.updateExtensionPlugins(this);
|
1902 | }
|
1903 | };
|
1904 | InputRulesExtension = __decorateClass([
|
1905 | extension({
|
1906 | defaultPriority: ExtensionPriority6.Default,
|
1907 | handlerKeys: ["shouldSkipInputRule"],
|
1908 | handlerKeyOptions: { shouldSkipInputRule: { earlyReturnValue: true } }
|
1909 | })
|
1910 | ], InputRulesExtension);
|
1911 |
|
1912 |
|
1913 | import { ExtensionPriority as ExtensionPriority7, ExtensionTag as ExtensionTag2, NamedShortcut as NamedShortcut2 } from "@remirror/core-constants";
|
1914 | import {
|
1915 | entries as entries4,
|
1916 | includes,
|
1917 | isArray,
|
1918 | isEmptyArray as isEmptyArray3,
|
1919 | isFunction as isFunction3,
|
1920 | isString as isString4,
|
1921 | isUndefined,
|
1922 | object as object6,
|
1923 | sort as sort2,
|
1924 | values
|
1925 | } from "@remirror/core-helpers";
|
1926 | import {
|
1927 | chainKeyBindingCommands,
|
1928 | convertCommand as convertCommand2,
|
1929 | environment as environment3,
|
1930 | findParentNodeOfType,
|
1931 | isDefaultBlockNode,
|
1932 | isEmptyBlockNode as isEmptyBlockNode2,
|
1933 | isEndOfTextBlock,
|
1934 | isStartOfDoc,
|
1935 | isStartOfTextBlock,
|
1936 | mergeProsemirrorKeyBindings
|
1937 | } from "@remirror/core-utils";
|
1938 | import {
|
1939 | baseKeymap,
|
1940 | chainCommands as pmChainCommands,
|
1941 | selectParentNode
|
1942 | } from "@remirror/pm/commands";
|
1943 | import { undoInputRule } from "@remirror/pm/inputrules";
|
1944 | import { keydownHandler } from "@remirror/pm/keymap";
|
1945 | import { Plugin } from "@remirror/pm/state";
|
1946 | var KeymapExtension = class extends PlainExtension {
|
1947 | constructor() {
|
1948 | super(...arguments);
|
1949 | __publicField(this, "extraKeyBindings", []);
|
1950 | __publicField(this, "backwardMarkExitTracker", new Map());
|
1951 | __publicField(this, "keydownHandler", null);
|
1952 | __publicField(this, "onAddCustomHandler", ({ keymap }) => {
|
1953 | var _a, _b;
|
1954 | if (!keymap) {
|
1955 | return;
|
1956 | }
|
1957 | this.extraKeyBindings = [...this.extraKeyBindings, keymap];
|
1958 | (_b = (_a = this.store).rebuildKeymap) == null ? void 0 : _b.call(_a);
|
1959 | return () => {
|
1960 | var _a2, _b2;
|
1961 | this.extraKeyBindings = this.extraKeyBindings.filter((binding) => binding !== keymap);
|
1962 | (_b2 = (_a2 = this.store).rebuildKeymap) == null ? void 0 : _b2.call(_a2);
|
1963 | };
|
1964 | });
|
1965 | __publicField(this, "rebuildKeymap", () => {
|
1966 | this.setupKeydownHandler();
|
1967 | });
|
1968 | }
|
1969 | get name() {
|
1970 | return "keymap";
|
1971 | }
|
1972 | get shortcutMap() {
|
1973 | const { shortcuts } = this.options;
|
1974 | return isString4(shortcuts) ? keyboardShortcuts[shortcuts] : shortcuts;
|
1975 | }
|
1976 | onCreate() {
|
1977 | this.store.setExtensionStore("rebuildKeymap", this.rebuildKeymap);
|
1978 | }
|
1979 | createExternalPlugins() {
|
1980 | var _a;
|
1981 | if ((_a = this.store.managerSettings.exclude) == null ? void 0 : _a.keymap) {
|
1982 | return [];
|
1983 | }
|
1984 | this.setupKeydownHandler();
|
1985 | return [
|
1986 | new Plugin({
|
1987 | props: {
|
1988 | handleKeyDown: (view, event) => {
|
1989 | var _a2;
|
1990 | return (_a2 = this.keydownHandler) == null ? void 0 : _a2.call(this, view, event);
|
1991 | }
|
1992 | }
|
1993 | })
|
1994 | ];
|
1995 | }
|
1996 | setupKeydownHandler() {
|
1997 | const bindings = this.generateKeymapBindings();
|
1998 | this.keydownHandler = keydownHandler(bindings);
|
1999 | }
|
2000 | generateKeymapBindings() {
|
2001 | var _a, _b, _c;
|
2002 | const extensionKeymaps = [];
|
2003 | const shortcutMap = this.shortcutMap;
|
2004 | const commandsExtension = this.store.getExtension(CommandsExtension);
|
2005 | const extractNamesFactory = (extension2) => (shortcut) => extractShortcutNames({
|
2006 | shortcut,
|
2007 | map: shortcutMap,
|
2008 | store: this.store,
|
2009 | options: extension2.options
|
2010 | });
|
2011 | for (const extension2 of this.store.extensions) {
|
2012 | const decoratedKeybindings = (_a = extension2.decoratedKeybindings) != null ? _a : {};
|
2013 | if ((_b = extension2.options.exclude) == null ? void 0 : _b.keymap) {
|
2014 | continue;
|
2015 | }
|
2016 | if (extension2.createKeymap) {
|
2017 | extensionKeymaps.push(updateNamedKeys(extension2.createKeymap(extractNamesFactory(extension2)), shortcutMap));
|
2018 | }
|
2019 | for (const [name, options] of entries4(decoratedKeybindings)) {
|
2020 | if (options.isActive && !options.isActive(extension2.options, this.store)) {
|
2021 | continue;
|
2022 | }
|
2023 | const keyBinding2 = extension2[name].bind(extension2);
|
2024 | const shortcutNames = extractShortcutNames({
|
2025 | shortcut: options.shortcut,
|
2026 | map: shortcutMap,
|
2027 | options: extension2.options,
|
2028 | store: this.store
|
2029 | });
|
2030 | const priority = isFunction3(options.priority) ? options.priority(extension2.options, this.store) : (_c = options.priority) != null ? _c : ExtensionPriority7.Low;
|
2031 | const bindingObject = object6();
|
2032 | for (const shortcut of shortcutNames) {
|
2033 | bindingObject[shortcut] = keyBinding2;
|
2034 | }
|
2035 | extensionKeymaps.push([priority, bindingObject]);
|
2036 | if (options.command) {
|
2037 | commandsExtension.updateDecorated(options.command, { shortcut: shortcutNames });
|
2038 | }
|
2039 | }
|
2040 | }
|
2041 | const sortedKeymaps = this.sortKeymaps([...this.extraKeyBindings, ...extensionKeymaps]);
|
2042 | const mappedCommands = mergeProsemirrorKeyBindings(sortedKeymaps);
|
2043 | return mappedCommands;
|
2044 | }
|
2045 | arrowRightShortcut(props) {
|
2046 | const excludedMarks = this.store.markTags[ExtensionTag2.PreventExits];
|
2047 | const excludedNodes = this.store.nodeTags[ExtensionTag2.PreventExits];
|
2048 | return this.exitMarkForwards(excludedMarks, excludedNodes)(props);
|
2049 | }
|
2050 | arrowLeftShortcut(props) {
|
2051 | const excludedMarks = this.store.markTags[ExtensionTag2.PreventExits];
|
2052 | const excludedNodes = this.store.nodeTags[ExtensionTag2.PreventExits];
|
2053 | return chainKeyBindingCommands(this.exitNodeBackwards(excludedNodes), this.exitMarkBackwards(excludedMarks, excludedNodes))(props);
|
2054 | }
|
2055 | backspace(props) {
|
2056 | const excludedMarks = this.store.markTags[ExtensionTag2.PreventExits];
|
2057 | const excludedNodes = this.store.nodeTags[ExtensionTag2.PreventExits];
|
2058 | return chainKeyBindingCommands(this.exitNodeBackwards(excludedNodes, true), this.exitMarkBackwards(excludedMarks, excludedNodes, true))(props);
|
2059 | }
|
2060 | createKeymap() {
|
2061 | const { selectParentNodeOnEscape, undoInputRuleOnBackspace, excludeBaseKeymap } = this.options;
|
2062 | const baseKeyBindings = object6();
|
2063 | if (!excludeBaseKeymap) {
|
2064 | for (const [key2, value] of entries4(baseKeymap)) {
|
2065 | baseKeyBindings[key2] = convertCommand2(value);
|
2066 | }
|
2067 | }
|
2068 | if (undoInputRuleOnBackspace && baseKeymap.Backspace) {
|
2069 | baseKeyBindings.Backspace = convertCommand2(pmChainCommands(undoInputRule, baseKeymap.Backspace));
|
2070 | }
|
2071 | if (selectParentNodeOnEscape) {
|
2072 | baseKeyBindings.Escape = convertCommand2(selectParentNode);
|
2073 | }
|
2074 | return [ExtensionPriority7.Low, baseKeyBindings];
|
2075 | }
|
2076 | getNamedShortcut(shortcut, options = {}) {
|
2077 | if (!shortcut.startsWith("_|")) {
|
2078 | return [shortcut];
|
2079 | }
|
2080 | return extractShortcutNames({
|
2081 | shortcut,
|
2082 | map: this.shortcutMap,
|
2083 | store: this.store,
|
2084 | options
|
2085 | });
|
2086 | }
|
2087 | onSetOptions(props) {
|
2088 | var _a, _b;
|
2089 | const { changes } = props;
|
2090 | if (changes.excludeBaseKeymap.changed || changes.selectParentNodeOnEscape.changed || changes.undoInputRuleOnBackspace.changed) {
|
2091 | (_b = (_a = this.store).rebuildKeymap) == null ? void 0 : _b.call(_a);
|
2092 | }
|
2093 | }
|
2094 | sortKeymaps(bindings) {
|
2095 | return sort2(bindings.map((binding) => isArray(binding) ? binding : [ExtensionPriority7.Default, binding]), (a, z) => z[0] - a[0]).map((binding) => binding[1]);
|
2096 | }
|
2097 | exitMarkForwards(excludedMarks, excludedNodes) {
|
2098 | return (props) => {
|
2099 | const { tr, dispatch } = props;
|
2100 | if (!isEndOfTextBlock(tr.selection)) {
|
2101 | return false;
|
2102 | }
|
2103 | const isInsideExcludedNode = findParentNodeOfType({
|
2104 | selection: tr.selection,
|
2105 | types: excludedNodes
|
2106 | });
|
2107 | if (isInsideExcludedNode) {
|
2108 | return false;
|
2109 | }
|
2110 | const $pos = tr.selection.$from;
|
2111 | const marksToRemove = $pos.marks().filter((mark) => !excludedMarks.includes(mark.type.name));
|
2112 | if (isEmptyArray3(marksToRemove)) {
|
2113 | return false;
|
2114 | }
|
2115 | if (!dispatch) {
|
2116 | return true;
|
2117 | }
|
2118 | for (const mark of marksToRemove) {
|
2119 | tr.removeStoredMark(mark);
|
2120 | }
|
2121 | dispatch(tr.insertText(" ", tr.selection.from));
|
2122 | return true;
|
2123 | };
|
2124 | }
|
2125 | exitNodeBackwards(excludedNodes, startOfDoc = false) {
|
2126 | return (props) => {
|
2127 | const { tr } = props;
|
2128 | const checker = startOfDoc ? isStartOfDoc : isStartOfTextBlock;
|
2129 | if (!checker(tr.selection)) {
|
2130 | return false;
|
2131 | }
|
2132 | const node = tr.selection.$anchor.node();
|
2133 | if (!isEmptyBlockNode2(node) || isDefaultBlockNode(node) || excludedNodes.includes(node.type.name)) {
|
2134 | return false;
|
2135 | }
|
2136 | return this.store.commands.toggleBlockNodeItem.original({ type: node.type })(props);
|
2137 | };
|
2138 | }
|
2139 | exitMarkBackwards(excludedMarks, excludedNodes, startOfDoc = false) {
|
2140 | return (props) => {
|
2141 | var _a;
|
2142 | const { tr, dispatch } = props;
|
2143 | const checker = startOfDoc ? isStartOfDoc : isStartOfTextBlock;
|
2144 | if (!checker(tr.selection) || this.backwardMarkExitTracker.has(tr.selection.anchor)) {
|
2145 | this.backwardMarkExitTracker.clear();
|
2146 | return false;
|
2147 | }
|
2148 | const isInsideExcludedNode = findParentNodeOfType({
|
2149 | selection: tr.selection,
|
2150 | types: excludedNodes
|
2151 | });
|
2152 | if (isInsideExcludedNode) {
|
2153 | return false;
|
2154 | }
|
2155 | const marksToRemove = [...(_a = tr.storedMarks) != null ? _a : [], ...tr.selection.$from.marks()].filter((mark) => !excludedMarks.includes(mark.type.name));
|
2156 | if (isEmptyArray3(marksToRemove)) {
|
2157 | return false;
|
2158 | }
|
2159 | if (!dispatch) {
|
2160 | return true;
|
2161 | }
|
2162 | for (const mark of marksToRemove) {
|
2163 | tr.removeStoredMark(mark);
|
2164 | }
|
2165 | this.backwardMarkExitTracker.set(tr.selection.anchor, true);
|
2166 | dispatch(tr);
|
2167 | return true;
|
2168 | };
|
2169 | }
|
2170 | };
|
2171 | __decorateClass([
|
2172 | keyBinding({
|
2173 | shortcut: "ArrowRight",
|
2174 | isActive: (options) => options.exitMarksOnArrowPress
|
2175 | })
|
2176 | ], KeymapExtension.prototype, "arrowRightShortcut", 1);
|
2177 | __decorateClass([
|
2178 | keyBinding({
|
2179 | shortcut: "ArrowLeft",
|
2180 | isActive: (options) => options.exitMarksOnArrowPress
|
2181 | })
|
2182 | ], KeymapExtension.prototype, "arrowLeftShortcut", 1);
|
2183 | __decorateClass([
|
2184 | keyBinding({
|
2185 | shortcut: "Backspace",
|
2186 | isActive: (options) => options.exitMarksOnArrowPress
|
2187 | })
|
2188 | ], KeymapExtension.prototype, "backspace", 1);
|
2189 | __decorateClass([
|
2190 | helper()
|
2191 | ], KeymapExtension.prototype, "getNamedShortcut", 1);
|
2192 | KeymapExtension = __decorateClass([
|
2193 | extension({
|
2194 | defaultPriority: ExtensionPriority7.Low,
|
2195 | defaultOptions: {
|
2196 | shortcuts: "default",
|
2197 | undoInputRuleOnBackspace: true,
|
2198 | selectParentNodeOnEscape: false,
|
2199 | excludeBaseKeymap: false,
|
2200 | exitMarksOnArrowPress: true
|
2201 | },
|
2202 | customHandlerKeys: ["keymap"]
|
2203 | })
|
2204 | ], KeymapExtension);
|
2205 | function isNamedShortcut(value) {
|
2206 | return includes(values(NamedShortcut2), value);
|
2207 | }
|
2208 | function extractShortcutNames({
|
2209 | shortcut,
|
2210 | map,
|
2211 | options,
|
2212 | store
|
2213 | }) {
|
2214 | if (isString4(shortcut)) {
|
2215 | return [normalizeShortcutName(shortcut, map)];
|
2216 | }
|
2217 | if (isArray(shortcut)) {
|
2218 | return shortcut.map((value) => normalizeShortcutName(value, map));
|
2219 | }
|
2220 | shortcut = shortcut(options, store);
|
2221 | return extractShortcutNames({ shortcut, map, options, store });
|
2222 | }
|
2223 | function normalizeShortcutName(value, shortcutMap) {
|
2224 | return isNamedShortcut(value) ? shortcutMap[value] : value;
|
2225 | }
|
2226 | function updateNamedKeys(prioritizedBindings, shortcutMap) {
|
2227 | const updatedBindings = {};
|
2228 | let previousBindings;
|
2229 | let priority;
|
2230 | if (isArray(prioritizedBindings)) {
|
2231 | [priority, previousBindings] = prioritizedBindings;
|
2232 | } else {
|
2233 | previousBindings = prioritizedBindings;
|
2234 | }
|
2235 | for (const [shortcutName, commandFunction] of entries4(previousBindings)) {
|
2236 | updatedBindings[normalizeShortcutName(shortcutName, shortcutMap)] = commandFunction;
|
2237 | }
|
2238 | return isUndefined(priority) ? updatedBindings : [priority, updatedBindings];
|
2239 | }
|
2240 | var DEFAULT_SHORTCUTS = {
|
2241 | [NamedShortcut2.Copy]: "Mod-c",
|
2242 | [NamedShortcut2.Cut]: "Mod-x",
|
2243 | [NamedShortcut2.Paste]: "Mod-v",
|
2244 | [NamedShortcut2.PastePlain]: "Mod-Shift-v",
|
2245 | [NamedShortcut2.SelectAll]: "Mod-a",
|
2246 | [NamedShortcut2.Undo]: "Mod-z",
|
2247 | [NamedShortcut2.Redo]: environment3.isMac ? "Shift-Mod-z" : "Mod-y",
|
2248 | [NamedShortcut2.Bold]: "Mod-b",
|
2249 | [NamedShortcut2.Italic]: "Mod-i",
|
2250 | [NamedShortcut2.Underline]: "Mod-u",
|
2251 | [NamedShortcut2.Strike]: "Mod-d",
|
2252 | [NamedShortcut2.Code]: "Mod-`",
|
2253 | [NamedShortcut2.Paragraph]: "Mod-Shift-0",
|
2254 | [NamedShortcut2.H1]: "Mod-Shift-1",
|
2255 | [NamedShortcut2.H2]: "Mod-Shift-2",
|
2256 | [NamedShortcut2.H3]: "Mod-Shift-3",
|
2257 | [NamedShortcut2.H4]: "Mod-Shift-4",
|
2258 | [NamedShortcut2.H5]: "Mod-Shift-5",
|
2259 | [NamedShortcut2.H6]: "Mod-Shift-6",
|
2260 | [NamedShortcut2.TaskList]: "Mod-Shift-7",
|
2261 | [NamedShortcut2.BulletList]: "Mod-Shift-8",
|
2262 | [NamedShortcut2.OrderedList]: "Mod-Shift-9",
|
2263 | [NamedShortcut2.Quote]: "Mod->",
|
2264 | [NamedShortcut2.Divider]: "Mod-Shift-|",
|
2265 | [NamedShortcut2.Codeblock]: "Mod-Shift-~",
|
2266 | [NamedShortcut2.ClearFormatting]: "Mod-Shift-C",
|
2267 | [NamedShortcut2.Superscript]: "Mod-.",
|
2268 | [NamedShortcut2.Subscript]: "Mod-,",
|
2269 | [NamedShortcut2.LeftAlignment]: "Mod-Shift-L",
|
2270 | [NamedShortcut2.CenterAlignment]: "Mod-Shift-E",
|
2271 | [NamedShortcut2.RightAlignment]: "Mod-Shift-R",
|
2272 | [NamedShortcut2.JustifyAlignment]: "Mod-Shift-J",
|
2273 | [NamedShortcut2.InsertLink]: "Mod-k",
|
2274 | [NamedShortcut2.Find]: "Mod-f",
|
2275 | [NamedShortcut2.FindBackwards]: "Mod-Shift-f",
|
2276 | [NamedShortcut2.FindReplace]: "Mod-Shift-H",
|
2277 | [NamedShortcut2.AddFootnote]: "Mod-Alt-f",
|
2278 | [NamedShortcut2.AddComment]: "Mod-Alt-m",
|
2279 | [NamedShortcut2.ContextMenu]: "Mod-Shift-\\",
|
2280 | [NamedShortcut2.IncreaseFontSize]: "Mod-Shift-.",
|
2281 | [NamedShortcut2.DecreaseFontSize]: "Mod-Shift-,",
|
2282 | [NamedShortcut2.IncreaseIndent]: "Tab",
|
2283 | [NamedShortcut2.DecreaseIndent]: "Shift-Tab",
|
2284 | [NamedShortcut2.Shortcuts]: "Mod-/",
|
2285 | [NamedShortcut2.Format]: environment3.isMac ? "Alt-Shift-f" : "Shift-Ctrl-f"
|
2286 | };
|
2287 | var GOOGLE_DOC_SHORTCUTS = {
|
2288 | ...DEFAULT_SHORTCUTS,
|
2289 | [NamedShortcut2.Strike]: "Mod-Shift-S",
|
2290 | [NamedShortcut2.Code]: "Mod-Shift-M",
|
2291 | [NamedShortcut2.Paragraph]: "Mod-Alt-0",
|
2292 | [NamedShortcut2.H1]: "Mod-Alt-1",
|
2293 | [NamedShortcut2.H2]: "Mod-Alt-2",
|
2294 | [NamedShortcut2.H3]: "Mod-Alt-3",
|
2295 | [NamedShortcut2.H4]: "Mod-Alt-4",
|
2296 | [NamedShortcut2.H5]: "Mod-Alt-5",
|
2297 | [NamedShortcut2.H6]: "Mod-Alt-6",
|
2298 | [NamedShortcut2.OrderedList]: "Mod-Alt-7",
|
2299 | [NamedShortcut2.BulletList]: "Mod-Alt-8",
|
2300 | [NamedShortcut2.Quote]: "Mod-Alt-9",
|
2301 | [NamedShortcut2.ClearFormatting]: "Mod-\\",
|
2302 | [NamedShortcut2.IncreaseIndent]: "Mod-[",
|
2303 | [NamedShortcut2.DecreaseIndent]: "Mod-]"
|
2304 | };
|
2305 | var keyboardShortcuts = {
|
2306 | default: DEFAULT_SHORTCUTS,
|
2307 | googleDoc: GOOGLE_DOC_SHORTCUTS
|
2308 | };
|
2309 |
|
2310 |
|
2311 | import { isFunction as isFunction4, object as object7 } from "@remirror/core-helpers";
|
2312 | var NodeViewsExtension = class extends PlainExtension {
|
2313 | get name() {
|
2314 | return "nodeViews";
|
2315 | }
|
2316 | createPlugin() {
|
2317 | var _a;
|
2318 | const nodeViewList = [];
|
2319 | const nodeViews = object7();
|
2320 | for (const extension2 of this.store.extensions) {
|
2321 | if (!extension2.createNodeViews) {
|
2322 | continue;
|
2323 | }
|
2324 | const nodeView = extension2.createNodeViews();
|
2325 | nodeViewList.unshift(isFunction4(nodeView) ? { [extension2.name]: nodeView } : nodeView);
|
2326 | }
|
2327 | nodeViewList.unshift((_a = this.store.managerSettings.nodeViews) != null ? _a : {});
|
2328 | for (const nodeView of nodeViewList) {
|
2329 | Object.assign(nodeViews, nodeView);
|
2330 | }
|
2331 | return {
|
2332 | props: { nodeViews }
|
2333 | };
|
2334 | }
|
2335 | };
|
2336 |
|
2337 |
|
2338 | import { isArray as isArray2 } from "@remirror/core-helpers";
|
2339 | import { pasteRules } from "@remirror/pm/paste-rules";
|
2340 | var PasteRulesExtension = class extends PlainExtension {
|
2341 | get name() {
|
2342 | return "pasteRules";
|
2343 | }
|
2344 | createExternalPlugins() {
|
2345 | return [this.generatePasteRulesPlugin()];
|
2346 | }
|
2347 | generatePasteRulesPlugin() {
|
2348 | var _a, _b;
|
2349 | const extensionPasteRules = [];
|
2350 | for (const extension2 of this.store.extensions) {
|
2351 | if (((_a = this.store.managerSettings.exclude) == null ? void 0 : _a.pasteRules) || !extension2.createPasteRules || ((_b = extension2.options.exclude) == null ? void 0 : _b.pasteRules)) {
|
2352 | continue;
|
2353 | }
|
2354 | const value = extension2.createPasteRules();
|
2355 | const rules = isArray2(value) ? value : [value];
|
2356 | extensionPasteRules.push(...rules);
|
2357 | }
|
2358 | return pasteRules(extensionPasteRules);
|
2359 | }
|
2360 | };
|
2361 |
|
2362 |
|
2363 | import { ErrorConstant as ErrorConstant7, ExtensionPriority as ExtensionPriority8, ManagerPhase } from "@remirror/core-constants";
|
2364 | import { assertGet as assertGet3, invariant as invariant6, isEmptyArray as isEmptyArray4, object as object8 } from "@remirror/core-helpers";
|
2365 | import { Plugin as Plugin2, PluginKey } from "@remirror/pm/state";
|
2366 | var PluginsExtension = class extends PlainExtension {
|
2367 | constructor() {
|
2368 | super(...arguments);
|
2369 | __publicField(this, "plugins", []);
|
2370 | __publicField(this, "managerPlugins", []);
|
2371 | __publicField(this, "applyStateHandlers", []);
|
2372 | __publicField(this, "initStateHandlers", []);
|
2373 | __publicField(this, "appendTransactionHandlers", []);
|
2374 | __publicField(this, "pluginKeys", object8());
|
2375 | __publicField(this, "stateGetters", new Map());
|
2376 | __publicField(this, "getPluginStateCreator", (key2) => (state) => {
|
2377 | return key2.getState(state != null ? state : this.store.getState());
|
2378 | });
|
2379 | __publicField(this, "getStateByName", (identifier) => {
|
2380 | const stateGetter = this.stateGetters.get(identifier);
|
2381 | invariant6(stateGetter, { message: "No plugin exists for the requested extension name." });
|
2382 | return stateGetter();
|
2383 | });
|
2384 | }
|
2385 | get name() {
|
2386 | return "plugins";
|
2387 | }
|
2388 | onCreate() {
|
2389 | const { setStoreKey, setExtensionStore, managerSettings, extensions } = this.store;
|
2390 | this.updateExtensionStore();
|
2391 | const { plugins = [] } = managerSettings;
|
2392 | this.updatePlugins(plugins, this.managerPlugins);
|
2393 | for (const extension2 of extensions) {
|
2394 | if (extension2.onApplyState) {
|
2395 | this.applyStateHandlers.push(extension2.onApplyState.bind(extension2));
|
2396 | }
|
2397 | if (extension2.onInitState) {
|
2398 | this.initStateHandlers.push(extension2.onInitState.bind(extension2));
|
2399 | }
|
2400 | if (extension2.onAppendTransaction) {
|
2401 | this.appendTransactionHandlers.push(extension2.onAppendTransaction.bind(extension2));
|
2402 | }
|
2403 | this.extractExtensionPlugins(extension2);
|
2404 | }
|
2405 | this.managerPlugins = plugins;
|
2406 | this.store.setStoreKey("plugins", this.plugins);
|
2407 | setStoreKey("pluginKeys", this.pluginKeys);
|
2408 | setStoreKey("getPluginState", this.getStateByName);
|
2409 | setExtensionStore("getPluginState", this.getStateByName);
|
2410 | }
|
2411 | createPlugin() {
|
2412 | return {
|
2413 | appendTransaction: (transactions, previousState, state) => {
|
2414 | const tr = state.tr;
|
2415 | const props = { previousState, tr, transactions, state };
|
2416 | for (const handler of this.appendTransactionHandlers) {
|
2417 | handler(props);
|
2418 | }
|
2419 | this.options.appendTransaction(props);
|
2420 | return tr.docChanged || tr.steps.length > 0 || tr.selectionSet || tr.storedMarksSet ? tr : void 0;
|
2421 | },
|
2422 | state: {
|
2423 | init: (_, state) => {
|
2424 | for (const handler of this.initStateHandlers) {
|
2425 | handler(state);
|
2426 | }
|
2427 | },
|
2428 | apply: (tr, _, previousState, state) => {
|
2429 | const props = { previousState, state, tr };
|
2430 | for (const handler of this.applyStateHandlers) {
|
2431 | handler(props);
|
2432 | }
|
2433 | this.options.applyState(props);
|
2434 | }
|
2435 | }
|
2436 | };
|
2437 | }
|
2438 | extractExtensionPlugins(extension2) {
|
2439 | var _a, _b;
|
2440 | const isNotPluginCreator = !extension2.createPlugin && !extension2.createExternalPlugins;
|
2441 | if (isNotPluginCreator || ((_a = this.store.managerSettings.exclude) == null ? void 0 : _a.plugins) || ((_b = extension2.options.exclude) == null ? void 0 : _b.plugins)) {
|
2442 | return;
|
2443 | }
|
2444 | if (extension2.createPlugin) {
|
2445 | const key2 = new PluginKey(extension2.name);
|
2446 | this.pluginKeys[extension2.name] = key2;
|
2447 | const getter = this.getPluginStateCreator(key2);
|
2448 | extension2.pluginKey = key2;
|
2449 | extension2.getPluginState = getter;
|
2450 | this.stateGetters.set(extension2.name, getter);
|
2451 | this.stateGetters.set(extension2.constructor, getter);
|
2452 | const pluginSpec = {
|
2453 | ...extension2.createPlugin(),
|
2454 | key: key2
|
2455 | };
|
2456 | const plugin = new Plugin2(pluginSpec);
|
2457 | this.updatePlugins([plugin], extension2.plugin ? [extension2.plugin] : void 0);
|
2458 | extension2.plugin = plugin;
|
2459 | }
|
2460 | if (extension2.createExternalPlugins) {
|
2461 | const externalPlugins = extension2.createExternalPlugins();
|
2462 | this.updatePlugins(externalPlugins, extension2.externalPlugins);
|
2463 | extension2.externalPlugins = externalPlugins;
|
2464 | }
|
2465 | }
|
2466 | updatePlugins(plugins, previous) {
|
2467 | if (!previous || isEmptyArray4(previous)) {
|
2468 | this.plugins = [...this.plugins, ...plugins];
|
2469 | return;
|
2470 | }
|
2471 | if (plugins.length !== previous.length) {
|
2472 | this.plugins = [...this.plugins.filter((plugin) => !previous.includes(plugin)), ...plugins];
|
2473 | return;
|
2474 | }
|
2475 | const pluginMap = new Map();
|
2476 | for (const [index, plugin] of plugins.entries()) {
|
2477 | pluginMap.set(assertGet3(previous, index), plugin);
|
2478 | }
|
2479 | this.plugins = this.plugins.map((plugin) => {
|
2480 | return previous.includes(plugin) ? pluginMap.get(plugin) : plugin;
|
2481 | });
|
2482 | }
|
2483 | updateExtensionStore() {
|
2484 | const { setExtensionStore } = this.store;
|
2485 | setExtensionStore("updatePlugins", this.updatePlugins.bind(this));
|
2486 | setExtensionStore("dispatchPluginUpdate", this.dispatchPluginUpdate.bind(this));
|
2487 | setExtensionStore("updateExtensionPlugins", this.updateExtensionPlugins.bind(this));
|
2488 | }
|
2489 | updateExtensionPlugins(value) {
|
2490 | const extension2 = isExtension(value) ? value : isExtensionConstructor(value) ? this.store.manager.getExtension(value) : this.store.extensions.find((extension3) => extension3.name === value);
|
2491 | invariant6(extension2, {
|
2492 | code: ErrorConstant7.INVALID_MANAGER_EXTENSION,
|
2493 | message: `The extension ${value} does not exist within the editor.`
|
2494 | });
|
2495 | this.extractExtensionPlugins(extension2);
|
2496 | this.store.setStoreKey("plugins", this.plugins);
|
2497 | this.dispatchPluginUpdate();
|
2498 | }
|
2499 | dispatchPluginUpdate() {
|
2500 | invariant6(this.store.phase >= ManagerPhase.EditorView, {
|
2501 | code: ErrorConstant7.MANAGER_PHASE_ERROR,
|
2502 | message: "`dispatchPluginUpdate` should only be called after the view has been added to the manager."
|
2503 | });
|
2504 | const { view, updateState } = this.store;
|
2505 | const newState = view.state.reconfigure({ plugins: this.plugins });
|
2506 | updateState(newState);
|
2507 | }
|
2508 | };
|
2509 | PluginsExtension = __decorateClass([
|
2510 | extension({
|
2511 | defaultPriority: ExtensionPriority8.Highest,
|
2512 | handlerKeys: ["applyState", "appendTransaction"]
|
2513 | })
|
2514 | ], PluginsExtension);
|
2515 |
|
2516 |
|
2517 | import {
|
2518 | ErrorConstant as ErrorConstant8,
|
2519 | ExtensionPriority as ExtensionPriority9,
|
2520 | ExtensionTag as ExtensionTag3
|
2521 | } from "@remirror/core-constants";
|
2522 | import {
|
2523 | assertGet as assertGet4,
|
2524 | entries as entries5,
|
2525 | invariant as invariant7,
|
2526 | isArray as isArray3,
|
2527 | isFunction as isFunction5,
|
2528 | isNullOrUndefined,
|
2529 | isPlainObject,
|
2530 | isString as isString5,
|
2531 | object as object9,
|
2532 | toString
|
2533 | } from "@remirror/core-helpers";
|
2534 | import {
|
2535 | getDefaultBlockNode,
|
2536 | getMarkRange as getMarkRange3,
|
2537 | isElementDomNode,
|
2538 | isProsemirrorMark,
|
2539 | isProsemirrorNode as isProsemirrorNode2
|
2540 | } from "@remirror/core-utils";
|
2541 | import { Schema } from "@remirror/pm/model";
|
2542 | import { ignoreUpdateForSuggest } from "@remirror/pm/suggest";
|
2543 | var SchemaExtension = class extends PlainExtension {
|
2544 | constructor() {
|
2545 | super(...arguments);
|
2546 | __publicField(this, "dynamicAttributes", {
|
2547 | marks: object9(),
|
2548 | nodes: object9()
|
2549 | });
|
2550 | }
|
2551 | get name() {
|
2552 | return "schema";
|
2553 | }
|
2554 | onCreate() {
|
2555 | var _a;
|
2556 | const { managerSettings, tags, markNames, nodeNames, extensions } = this.store;
|
2557 | const { defaultBlockNode, disableExtraAttributes, nodeOverride, markOverride } = managerSettings;
|
2558 | const isValidDefaultBlockNode = (name) => !!(name && tags[ExtensionTag3.Block].includes(name));
|
2559 | if (managerSettings.schema) {
|
2560 | const { nodes: nodes2, marks: marks2 } = getSpecFromSchema(managerSettings.schema);
|
2561 | this.addSchema(managerSettings.schema, nodes2, marks2);
|
2562 | return;
|
2563 | }
|
2564 | const nodes = isValidDefaultBlockNode(defaultBlockNode) ? {
|
2565 | doc: object9(),
|
2566 | [defaultBlockNode]: object9()
|
2567 | } : object9();
|
2568 | const marks = object9();
|
2569 | const namedExtraAttributes = getNamedSchemaAttributes({
|
2570 | settings: managerSettings,
|
2571 | gatheredSchemaAttributes: this.gatherExtraAttributes(extensions),
|
2572 | nodeNames,
|
2573 | markNames,
|
2574 | tags
|
2575 | });
|
2576 | for (const extension2 of extensions) {
|
2577 | namedExtraAttributes[extension2.name] = {
|
2578 | ...namedExtraAttributes[extension2.name],
|
2579 | ...extension2.options.extraAttributes
|
2580 | };
|
2581 | const ignoreExtraAttributes = disableExtraAttributes === true || extension2.options.disableExtraAttributes === true || extension2.constructor.disableExtraAttributes === true;
|
2582 | if (isNodeExtension(extension2)) {
|
2583 | const { spec, dynamic } = createSpec({
|
2584 | createExtensionSpec: (extra, override) => extension2.createNodeSpec(extra, override),
|
2585 | extraAttributes: assertGet4(namedExtraAttributes, extension2.name),
|
2586 | override: { ...nodeOverride, ...extension2.options.nodeOverride },
|
2587 | ignoreExtraAttributes,
|
2588 | name: extension2.constructorName,
|
2589 | tags: extension2.tags
|
2590 | });
|
2591 | extension2.spec = spec;
|
2592 | nodes[extension2.name] = spec;
|
2593 | if (Object.keys(dynamic).length > 0) {
|
2594 | this.dynamicAttributes.nodes[extension2.name] = dynamic;
|
2595 | }
|
2596 | }
|
2597 | if (isMarkExtension(extension2)) {
|
2598 | const { spec, dynamic } = createSpec({
|
2599 | createExtensionSpec: (extra, override) => extension2.createMarkSpec(extra, override),
|
2600 | extraAttributes: assertGet4(namedExtraAttributes, extension2.name),
|
2601 | override: { ...markOverride, ...extension2.options.markOverride },
|
2602 | ignoreExtraAttributes,
|
2603 | name: extension2.constructorName,
|
2604 | tags: (_a = extension2.tags) != null ? _a : []
|
2605 | });
|
2606 | extension2.spec = spec;
|
2607 | marks[extension2.name] = spec;
|
2608 | if (Object.keys(dynamic).length > 0) {
|
2609 | this.dynamicAttributes.marks[extension2.name] = dynamic;
|
2610 | }
|
2611 | }
|
2612 | }
|
2613 | const schema = new Schema({ nodes, marks, topNode: "doc" });
|
2614 | this.addSchema(schema, nodes, marks);
|
2615 | }
|
2616 | createPlugin() {
|
2617 | return {
|
2618 | appendTransaction: (transactions, _, nextState) => {
|
2619 | const { tr } = nextState;
|
2620 | const documentHasChanged = transactions.some((tr2) => tr2.docChanged);
|
2621 | if (!documentHasChanged) {
|
2622 | return null;
|
2623 | }
|
2624 | if (Object.keys(this.dynamicAttributes.nodes).length === 0 && Object.keys(this.dynamicAttributes.marks).length === 0) {
|
2625 | return null;
|
2626 | }
|
2627 | tr.doc.descendants((child, pos) => {
|
2628 | this.checkAndUpdateDynamicNodes(child, pos, tr);
|
2629 | this.checkAndUpdateDynamicMarks(child, pos, tr);
|
2630 | return true;
|
2631 | });
|
2632 | return tr.steps.length > 0 ? tr : null;
|
2633 | }
|
2634 | };
|
2635 | }
|
2636 | addSchema(schema, nodes, marks) {
|
2637 | this.store.setStoreKey("nodes", nodes);
|
2638 | this.store.setStoreKey("marks", marks);
|
2639 | this.store.setStoreKey("schema", schema);
|
2640 | this.store.setExtensionStore("schema", schema);
|
2641 | this.store.setStoreKey("defaultBlockNode", getDefaultBlockNode(schema).name);
|
2642 | for (const type of Object.values(schema.nodes)) {
|
2643 | if (type.name === "doc") {
|
2644 | continue;
|
2645 | }
|
2646 | if (type.isBlock || type.isTextblock) {
|
2647 | break;
|
2648 | }
|
2649 | }
|
2650 | }
|
2651 | checkAndUpdateDynamicNodes(node, pos, tr) {
|
2652 | for (const [name, dynamic] of entries5(this.dynamicAttributes.nodes)) {
|
2653 | if (node.type.name !== name) {
|
2654 | continue;
|
2655 | }
|
2656 | for (const [attributeName, attributeCreator] of entries5(dynamic)) {
|
2657 | if (!isNullOrUndefined(node.attrs[attributeName])) {
|
2658 | continue;
|
2659 | }
|
2660 | const attrs = { ...node.attrs, [attributeName]: attributeCreator(node) };
|
2661 | tr.setNodeMarkup(pos, void 0, attrs);
|
2662 | ignoreUpdateForSuggest(tr);
|
2663 | }
|
2664 | }
|
2665 | }
|
2666 | checkAndUpdateDynamicMarks(node, pos, tr) {
|
2667 | for (const [name, dynamic] of entries5(this.dynamicAttributes.marks)) {
|
2668 | const type = assertGet4(this.store.schema.marks, name);
|
2669 | const mark = node.marks.find((mark2) => mark2.type.name === name);
|
2670 | if (!mark) {
|
2671 | continue;
|
2672 | }
|
2673 | for (const [attributeName, attributeCreator] of entries5(dynamic)) {
|
2674 | if (!isNullOrUndefined(mark.attrs[attributeName])) {
|
2675 | continue;
|
2676 | }
|
2677 | const range = getMarkRange3(tr.doc.resolve(pos), type);
|
2678 | if (!range) {
|
2679 | continue;
|
2680 | }
|
2681 | const { from, to } = range;
|
2682 | const newMark = type.create({
|
2683 | ...mark.attrs,
|
2684 | [attributeName]: attributeCreator(mark)
|
2685 | });
|
2686 | tr.removeMark(from, to, type).addMark(from, to, newMark);
|
2687 | ignoreUpdateForSuggest(tr);
|
2688 | }
|
2689 | }
|
2690 | }
|
2691 | gatherExtraAttributes(extensions) {
|
2692 | const extraSchemaAttributes = [];
|
2693 | for (const extension2 of extensions) {
|
2694 | if (!extension2.createSchemaAttributes) {
|
2695 | continue;
|
2696 | }
|
2697 | extraSchemaAttributes.push(...extension2.createSchemaAttributes());
|
2698 | }
|
2699 | return extraSchemaAttributes;
|
2700 | }
|
2701 | };
|
2702 | SchemaExtension = __decorateClass([
|
2703 | extension({ defaultPriority: ExtensionPriority9.Highest })
|
2704 | ], SchemaExtension);
|
2705 | function getNamedSchemaAttributes(props) {
|
2706 | var _a, _b;
|
2707 | const { settings, gatheredSchemaAttributes, nodeNames, markNames, tags } = props;
|
2708 | const extraAttributes = object9();
|
2709 | if (settings.disableExtraAttributes) {
|
2710 | return extraAttributes;
|
2711 | }
|
2712 | const extraSchemaAttributes = [
|
2713 | ...gatheredSchemaAttributes,
|
2714 | ...(_a = settings.extraAttributes) != null ? _a : []
|
2715 | ];
|
2716 | for (const attributeGroup of extraSchemaAttributes != null ? extraSchemaAttributes : []) {
|
2717 | const identifiers = getIdentifiers({
|
2718 | identifiers: attributeGroup.identifiers,
|
2719 | nodeNames,
|
2720 | markNames,
|
2721 | tags
|
2722 | });
|
2723 | for (const identifier of identifiers) {
|
2724 | const currentValue = (_b = extraAttributes[identifier]) != null ? _b : {};
|
2725 | extraAttributes[identifier] = { ...currentValue, ...attributeGroup.attributes };
|
2726 | }
|
2727 | }
|
2728 | return extraAttributes;
|
2729 | }
|
2730 | function isIdentifiersObject(value) {
|
2731 | return isPlainObject(value) && isArray3(value.tags);
|
2732 | }
|
2733 | function getIdentifiers(props) {
|
2734 | var _a;
|
2735 | const { identifiers, nodeNames, markNames, tags } = props;
|
2736 | if (identifiers === "nodes") {
|
2737 | return nodeNames;
|
2738 | }
|
2739 | if (identifiers === "marks") {
|
2740 | return markNames;
|
2741 | }
|
2742 | if (identifiers === "all") {
|
2743 | return [...nodeNames, ...markNames];
|
2744 | }
|
2745 | if (isArray3(identifiers)) {
|
2746 | return identifiers;
|
2747 | }
|
2748 | invariant7(isIdentifiersObject(identifiers), {
|
2749 | code: ErrorConstant8.EXTENSION_EXTRA_ATTRIBUTES,
|
2750 | message: `Invalid value passed as an identifier when creating \`extraAttributes\`.`
|
2751 | });
|
2752 | const {
|
2753 | tags: extensionTags = [],
|
2754 | names: extensionNames = [],
|
2755 | behavior = "any",
|
2756 | excludeNames,
|
2757 | excludeTags,
|
2758 | type
|
2759 | } = identifiers;
|
2760 | const names = new Set();
|
2761 | const acceptableNames = type === "mark" ? markNames : type === "node" ? nodeNames : [...markNames, ...nodeNames];
|
2762 | const isNameValid = (name) => acceptableNames.includes(name) && !(excludeNames == null ? void 0 : excludeNames.includes(name));
|
2763 | for (const name of extensionNames) {
|
2764 | if (isNameValid(name)) {
|
2765 | names.add(name);
|
2766 | }
|
2767 | }
|
2768 | const taggedNamesMap = new Map();
|
2769 | for (const tag of extensionTags) {
|
2770 | if (excludeTags == null ? void 0 : excludeTags.includes(tag)) {
|
2771 | continue;
|
2772 | }
|
2773 | for (const name of tags[tag]) {
|
2774 | if (!isNameValid(name)) {
|
2775 | continue;
|
2776 | }
|
2777 | if (behavior === "any") {
|
2778 | names.add(name);
|
2779 | continue;
|
2780 | }
|
2781 | const tagSet = (_a = taggedNamesMap.get(name)) != null ? _a : new Set();
|
2782 | tagSet.add(tag);
|
2783 | taggedNamesMap.set(name, tagSet);
|
2784 | }
|
2785 | }
|
2786 | for (const [name, tagSet] of taggedNamesMap) {
|
2787 | if (tagSet.size === extensionTags.length) {
|
2788 | names.add(name);
|
2789 | }
|
2790 | }
|
2791 | return [...names];
|
2792 | }
|
2793 | function createSpec(props) {
|
2794 | var _a, _b;
|
2795 | const { createExtensionSpec, extraAttributes, ignoreExtraAttributes, name, tags, override } = props;
|
2796 | const dynamic = object9();
|
2797 | function addDynamic(attributeName, creator) {
|
2798 | dynamic[attributeName] = creator;
|
2799 | }
|
2800 | let defaultsCalled = false;
|
2801 | function onDefaultsCalled() {
|
2802 | defaultsCalled = true;
|
2803 | }
|
2804 | const defaults = createDefaults(extraAttributes, ignoreExtraAttributes, onDefaultsCalled, addDynamic);
|
2805 | const parse = createParseDOM(extraAttributes, ignoreExtraAttributes);
|
2806 | const dom = createToDOM(extraAttributes, ignoreExtraAttributes);
|
2807 | const spec = createExtensionSpec({ defaults, parse, dom }, override);
|
2808 | invariant7(ignoreExtraAttributes || defaultsCalled, {
|
2809 | code: ErrorConstant8.EXTENSION_SPEC,
|
2810 | message: `When creating a node specification you must call the 'defaults', and parse, and 'dom' methods. To avoid this error you can set the static property 'disableExtraAttributes' of '${name}' to 'true'.`
|
2811 | });
|
2812 | spec.group = [...(_b = (_a = spec.group) == null ? void 0 : _a.split(" ")) != null ? _b : [], ...tags].join(" ") || void 0;
|
2813 | return { spec, dynamic };
|
2814 | }
|
2815 | function getExtraAttributesObject(value) {
|
2816 | if (isString5(value) || isFunction5(value)) {
|
2817 | return { default: value };
|
2818 | }
|
2819 | invariant7(value, {
|
2820 | message: `${toString(value)} is not supported`,
|
2821 | code: ErrorConstant8.EXTENSION_EXTRA_ATTRIBUTES
|
2822 | });
|
2823 | return value;
|
2824 | }
|
2825 | function createDefaults(extraAttributes, shouldIgnore, onCalled, addDynamicCreator) {
|
2826 | return () => {
|
2827 | onCalled();
|
2828 | const attributes = object9();
|
2829 | if (shouldIgnore) {
|
2830 | return attributes;
|
2831 | }
|
2832 | for (const [name, config] of entries5(extraAttributes)) {
|
2833 | const attributesObject = getExtraAttributesObject(config);
|
2834 | let defaultValue = attributesObject.default;
|
2835 | if (isFunction5(defaultValue)) {
|
2836 | addDynamicCreator(name, defaultValue);
|
2837 | defaultValue = null;
|
2838 | }
|
2839 | attributes[name] = defaultValue === void 0 ? {} : { default: defaultValue };
|
2840 | }
|
2841 | return attributes;
|
2842 | };
|
2843 | }
|
2844 | function createParseDOM(extraAttributes, shouldIgnore) {
|
2845 | return (domNode) => {
|
2846 | var _a, _b, _c;
|
2847 | const attributes = object9();
|
2848 | if (shouldIgnore) {
|
2849 | return attributes;
|
2850 | }
|
2851 | for (const [name, config] of entries5(extraAttributes)) {
|
2852 | const { parseDOM, ...other } = getExtraAttributesObject(config);
|
2853 | if (!isElementDomNode(domNode)) {
|
2854 | continue;
|
2855 | }
|
2856 | if (isNullOrUndefined(parseDOM)) {
|
2857 | attributes[name] = (_a = domNode.getAttribute(name)) != null ? _a : other.default;
|
2858 | continue;
|
2859 | }
|
2860 | if (isFunction5(parseDOM)) {
|
2861 | attributes[name] = (_b = parseDOM(domNode)) != null ? _b : other.default;
|
2862 | continue;
|
2863 | }
|
2864 | attributes[name] = (_c = domNode.getAttribute(parseDOM)) != null ? _c : other.default;
|
2865 | }
|
2866 | return attributes;
|
2867 | };
|
2868 | }
|
2869 | function createToDOM(extraAttributes, shouldIgnore) {
|
2870 | return (item) => {
|
2871 | const domAttributes = object9();
|
2872 | if (shouldIgnore) {
|
2873 | return domAttributes;
|
2874 | }
|
2875 | function updateDomAttributes(value, name) {
|
2876 | if (!value) {
|
2877 | return;
|
2878 | }
|
2879 | if (isString5(value)) {
|
2880 | domAttributes[name] = value;
|
2881 | return;
|
2882 | }
|
2883 | if (isArray3(value)) {
|
2884 | const [attr, val] = value;
|
2885 | domAttributes[attr] = val != null ? val : item.attrs[name];
|
2886 | return;
|
2887 | }
|
2888 | for (const [attr, val] of entries5(value)) {
|
2889 | domAttributes[attr] = val;
|
2890 | }
|
2891 | }
|
2892 | for (const [name, config] of entries5(extraAttributes)) {
|
2893 | const { toDOM, parseDOM } = getExtraAttributesObject(config);
|
2894 | if (isNullOrUndefined(toDOM)) {
|
2895 | const key2 = isString5(parseDOM) ? parseDOM : name;
|
2896 | domAttributes[key2] = item.attrs[name];
|
2897 | continue;
|
2898 | }
|
2899 | if (isFunction5(toDOM)) {
|
2900 | updateDomAttributes(toDOM(item.attrs, getNodeMarkOptions(item)), name);
|
2901 | continue;
|
2902 | }
|
2903 | updateDomAttributes(toDOM, name);
|
2904 | }
|
2905 | return domAttributes;
|
2906 | };
|
2907 | }
|
2908 | function getNodeMarkOptions(item) {
|
2909 | if (isProsemirrorNode2(item)) {
|
2910 | return { node: item };
|
2911 | }
|
2912 | if (isProsemirrorMark(item)) {
|
2913 | return { mark: item };
|
2914 | }
|
2915 | return {};
|
2916 | }
|
2917 | function getSpecFromSchema(schema) {
|
2918 | const nodes = object9();
|
2919 | const marks = object9();
|
2920 | for (const [name, type] of Object.entries(schema.nodes)) {
|
2921 | nodes[name] = type.spec;
|
2922 | }
|
2923 | for (const [name, type] of Object.entries(schema.marks)) {
|
2924 | marks[name] = type.spec;
|
2925 | }
|
2926 | return { nodes, marks };
|
2927 | }
|
2928 |
|
2929 |
|
2930 | import { includes as includes2, isArray as isArray4 } from "@remirror/core-helpers";
|
2931 | import {
|
2932 | addSuggester,
|
2933 | getSuggestPluginState,
|
2934 | removeSuggester,
|
2935 | suggest
|
2936 | } from "@remirror/pm/suggest";
|
2937 | var SuggestExtension = class extends PlainExtension {
|
2938 | constructor() {
|
2939 | super(...arguments);
|
2940 | __publicField(this, "onAddCustomHandler", ({ suggester }) => {
|
2941 | var _a;
|
2942 | if (!suggester || ((_a = this.store.managerSettings.exclude) == null ? void 0 : _a.suggesters)) {
|
2943 | return;
|
2944 | }
|
2945 | return addSuggester(this.store.getState(), suggester);
|
2946 | });
|
2947 | }
|
2948 | get name() {
|
2949 | return "suggest";
|
2950 | }
|
2951 | onCreate() {
|
2952 | this.store.setExtensionStore("addSuggester", (suggester) => addSuggester(this.store.getState(), suggester));
|
2953 | this.store.setExtensionStore("removeSuggester", (suggester) => removeSuggester(this.store.getState(), suggester));
|
2954 | }
|
2955 | createExternalPlugins() {
|
2956 | var _a, _b;
|
2957 | const suggesters = [];
|
2958 | for (const extension2 of this.store.extensions) {
|
2959 | if ((_a = this.store.managerSettings.exclude) == null ? void 0 : _a.suggesters) {
|
2960 | break;
|
2961 | }
|
2962 | if (!extension2.createSuggesters || ((_b = extension2.options.exclude) == null ? void 0 : _b.suggesters)) {
|
2963 | continue;
|
2964 | }
|
2965 | const suggester = extension2.createSuggesters();
|
2966 | const suggesterList = isArray4(suggester) ? suggester : [suggester];
|
2967 | suggesters.push(...suggesterList);
|
2968 | }
|
2969 | return [suggest(...suggesters)];
|
2970 | }
|
2971 | getSuggestState(state) {
|
2972 | return getSuggestPluginState(state != null ? state : this.store.getState());
|
2973 | }
|
2974 | getSuggestMethods() {
|
2975 | const {
|
2976 | addIgnored,
|
2977 | clearIgnored,
|
2978 | removeIgnored,
|
2979 | ignoreNextExit,
|
2980 | setMarkRemoved,
|
2981 | findMatchAtPosition,
|
2982 | findNextTextSelection,
|
2983 | setLastChangeFromAppend
|
2984 | } = this.getSuggestState();
|
2985 | return {
|
2986 | addIgnored,
|
2987 | clearIgnored,
|
2988 | removeIgnored,
|
2989 | ignoreNextExit,
|
2990 | setMarkRemoved,
|
2991 | findMatchAtPosition,
|
2992 | findNextTextSelection,
|
2993 | setLastChangeFromAppend
|
2994 | };
|
2995 | }
|
2996 | isSuggesterActive(name) {
|
2997 | var _a;
|
2998 | return includes2(isArray4(name) ? name : [name], (_a = this.getSuggestState().match) == null ? void 0 : _a.suggester.name);
|
2999 | }
|
3000 | };
|
3001 | __decorateClass([
|
3002 | helper()
|
3003 | ], SuggestExtension.prototype, "getSuggestState", 1);
|
3004 | __decorateClass([
|
3005 | helper()
|
3006 | ], SuggestExtension.prototype, "getSuggestMethods", 1);
|
3007 | __decorateClass([
|
3008 | helper()
|
3009 | ], SuggestExtension.prototype, "isSuggesterActive", 1);
|
3010 | SuggestExtension = __decorateClass([
|
3011 | extension({ customHandlerKeys: ["suggester"] })
|
3012 | ], SuggestExtension);
|
3013 |
|
3014 |
|
3015 | import {
|
3016 | ErrorConstant as ErrorConstant9,
|
3017 | ExtensionPriority as ExtensionPriority10,
|
3018 | ExtensionTag as ExtensionTag4
|
3019 | } from "@remirror/core-constants";
|
3020 | import { includes as includes3, invariant as invariant8, object as object10, values as values2 } from "@remirror/core-helpers";
|
3021 | var TagsExtension = class extends PlainExtension {
|
3022 | constructor() {
|
3023 | super(...arguments);
|
3024 | __publicField(this, "allTags", object10());
|
3025 | __publicField(this, "plainTags", object10());
|
3026 | __publicField(this, "markTags", object10());
|
3027 | __publicField(this, "nodeTags", object10());
|
3028 | }
|
3029 | get name() {
|
3030 | return "tags";
|
3031 | }
|
3032 | onCreate() {
|
3033 | this.resetTags();
|
3034 | for (const extension2 of this.store.extensions) {
|
3035 | this.updateTagForExtension(extension2);
|
3036 | }
|
3037 | this.store.setStoreKey("tags", this.allTags);
|
3038 | this.store.setExtensionStore("tags", this.allTags);
|
3039 | this.store.setStoreKey("plainTags", this.plainTags);
|
3040 | this.store.setExtensionStore("plainTags", this.plainTags);
|
3041 | this.store.setStoreKey("markTags", this.markTags);
|
3042 | this.store.setExtensionStore("markTags", this.markTags);
|
3043 | this.store.setStoreKey("nodeTags", this.nodeTags);
|
3044 | this.store.setExtensionStore("nodeTags", this.nodeTags);
|
3045 | }
|
3046 | resetTags() {
|
3047 | const allTags = object10();
|
3048 | const plainTags = object10();
|
3049 | const markTags = object10();
|
3050 | const nodeTags = object10();
|
3051 | for (const tagName of values2(ExtensionTag4)) {
|
3052 | allTags[tagName] = [];
|
3053 | plainTags[tagName] = [];
|
3054 | markTags[tagName] = [];
|
3055 | nodeTags[tagName] = [];
|
3056 | }
|
3057 | this.allTags = allTags;
|
3058 | this.plainTags = plainTags;
|
3059 | this.markTags = markTags;
|
3060 | this.nodeTags = nodeTags;
|
3061 | }
|
3062 | updateTagForExtension(extension2) {
|
3063 | var _a, _b, _c, _d, _e, _f;
|
3064 | const allTags = new Set([
|
3065 | ...(_a = extension2.tags) != null ? _a : [],
|
3066 | ...(_c = (_b = extension2.createTags) == null ? void 0 : _b.call(extension2)) != null ? _c : [],
|
3067 | ...(_d = extension2.options.extraTags) != null ? _d : [],
|
3068 | ...(_f = (_e = this.store.managerSettings.extraTags) == null ? void 0 : _e[extension2.name]) != null ? _f : []
|
3069 | ]);
|
3070 | for (const tag of allTags) {
|
3071 | invariant8(isExtensionTag(tag), {
|
3072 | code: ErrorConstant9.EXTENSION,
|
3073 | message: `The tag provided by the extension: ${extension2.constructorName} is not supported by the editor. To add custom tags you can use the 'mutateTag' method.`
|
3074 | });
|
3075 | this.allTags[tag].push(extension2.name);
|
3076 | if (isPlainExtension(extension2)) {
|
3077 | this.plainTags[tag].push(extension2.name);
|
3078 | }
|
3079 | if (isMarkExtension(extension2)) {
|
3080 | this.markTags[tag].push(extension2.name);
|
3081 | }
|
3082 | if (isNodeExtension(extension2)) {
|
3083 | this.nodeTags[tag].push(extension2.name);
|
3084 | }
|
3085 | }
|
3086 | extension2.tags = [...allTags];
|
3087 | }
|
3088 | };
|
3089 | TagsExtension = __decorateClass([
|
3090 | extension({ defaultPriority: ExtensionPriority10.Highest })
|
3091 | ], TagsExtension);
|
3092 | function isExtensionTag(value) {
|
3093 | return includes3(values2(ExtensionTag4), value);
|
3094 | }
|
3095 |
|
3096 |
|
3097 | import { Plugin as Plugin3, PluginKey as PluginKey2 } from "@remirror/pm/state";
|
3098 | import { Decoration as Decoration2, DecorationSet as DecorationSet2 } from "@remirror/pm/view";
|
3099 | var key = new PluginKey2("remirrorFilePlaceholderPlugin");
|
3100 | function createUploadPlaceholderPlugin() {
|
3101 | const plugin = new Plugin3({
|
3102 | key,
|
3103 | state: {
|
3104 | init() {
|
3105 | return { set: DecorationSet2.empty, payloads: new Map() };
|
3106 | },
|
3107 | apply(tr, { set, payloads }) {
|
3108 | set = set.map(tr.mapping, tr.doc);
|
3109 | const action = tr.getMeta(plugin);
|
3110 | if (action) {
|
3111 | if (action.type === 0 ) {
|
3112 | const widget = document.createElement("placeholder");
|
3113 | const deco = Decoration2.widget(action.pos, widget, { id: action.id });
|
3114 | set = set.add(tr.doc, [deco]);
|
3115 | payloads.set(action.id, action.payload);
|
3116 | } else if (action.type === 1 /* REMOVE_PLACEHOLDER */) {
|
3117 | set = set.remove(set.find(void 0, void 0, (spec) => spec.id === action.id));
|
3118 | payloads.delete(action.id);
|
3119 | }
|
3120 | }
|
3121 | return { set, payloads };
|
3122 | }
|
3123 | },
|
3124 | props: {
|
3125 | decorations(state) {
|
3126 | var _a, _b;
|
3127 | return (_b = (_a = plugin.getState(state)) == null ? void 0 : _a.set) != null ? _b : null;
|
3128 | }
|
3129 | }
|
3130 | });
|
3131 | return plugin;
|
3132 | }
|
3133 | function findUploadPlaceholderPos(state, id) {
|
3134 | var _a, _b;
|
3135 | const set = (_a = key.getState(state)) == null ? void 0 : _a.set;
|
3136 | if (set) {
|
3137 | const decorations = set.find(void 0, void 0, (spec) => spec.id === id);
|
3138 | const pos = (_b = decorations == null ? void 0 : decorations[0]) == null ? void 0 : _b.from;
|
3139 | if (pos != null) {
|
3140 | return pos;
|
3141 | }
|
3142 | }
|
3143 | let foundPos;
|
3144 | state.doc.descendants((node, pos) => {
|
3145 | if (node.attrs.id === id) {
|
3146 | foundPos = pos;
|
3147 | }
|
3148 | return foundPos === void 0;
|
3149 | });
|
3150 | return foundPos;
|
3151 | }
|
3152 | function findUploadPlaceholderPayload(state, id) {
|
3153 | var _a;
|
3154 | const payloads = (_a = key.getState(state)) == null ? void 0 : _a.payloads;
|
3155 | if (!payloads) {
|
3156 | return void 0;
|
3157 | }
|
3158 | return payloads.get(id);
|
3159 | }
|
3160 | function hasUploadingFile(state) {
|
3161 | var _a, _b, _c;
|
3162 | const placeholderCount = (_c = (_b = (_a = key.getState(state)) == null ? void 0 : _a.payloads) == null ? void 0 : _b.size) != null ? _c : 0;
|
3163 | return placeholderCount > 0;
|
3164 | }
|
3165 | function setUploadPlaceholderAction(tr, action) {
|
3166 | return tr.setMeta(key, action);
|
3167 | }
|
3168 |
|
3169 |
|
3170 | import { isNumber as isNumber2, uniqueId as uniqueId2 } from "@remirror/core-helpers";
|
3171 |
|
3172 |
|
3173 | import { createNanoEvents } from "nanoevents";
|
3174 | function createUploadContext() {
|
3175 | const values3 = {};
|
3176 | const emitter = createNanoEvents();
|
3177 | const get = (key2) => {
|
3178 | return values3[key2];
|
3179 | };
|
3180 | const set = (key2, value) => {
|
3181 | values3[key2] = value;
|
3182 | emitter.emit("set", values3);
|
3183 | };
|
3184 | const addListener = (listener) => {
|
3185 | return emitter.on("set", listener);
|
3186 | };
|
3187 | return { set, get, addListener };
|
3188 | }
|
3189 |
|
3190 | // packages/remirror__core/src/builtins/upload-extension/file-upload.ts
|
3191 | function uploadFile({
|
3192 | file,
|
3193 | pos,
|
3194 | view,
|
3195 | fileType,
|
3196 | uploadHandler
|
3197 | }) {
|
3198 | const id = uniqueId2("file-placeholder-");
|
3199 | const context = createUploadContext();
|
3200 | const fileUploader = createFilePlaceholder({
|
3201 | id,
|
3202 | context,
|
3203 | file,
|
3204 | pos,
|
3205 | view,
|
3206 | fileType,
|
3207 | uploadHandler
|
3208 | });
|
3209 | fileUploader == null ? void 0 : fileUploader.upload(context).then((attrs) => onFileLoaded({ id, fileType, view, attrs })).catch((error) => onFileLoaded({ id, fileType, view, attrs: { error: error.message } }));
|
3210 | }
|
3211 | function insertFilePoint(doc, pos, nodeType) {
|
3212 | const $pos = doc.resolve(pos);
|
3213 | if ($pos.parent.canReplaceWith($pos.index(), $pos.index(), nodeType)) {
|
3214 | return pos;
|
3215 | }
|
3216 | if ($pos.parentOffset === 0) {
|
3217 | for (let d = $pos.depth - 1; d >= 0; d--) {
|
3218 | const index = $pos.index(d);
|
3219 | if ($pos.node(d).canReplaceWith(index, index, nodeType)) {
|
3220 | return $pos.before(d + 1);
|
3221 | }
|
3222 | if (index > 0) {
|
3223 | return null;
|
3224 | }
|
3225 | }
|
3226 | }
|
3227 | for (let d = $pos.depth - 1; d >= 0; d--) {
|
3228 | const index = $pos.indexAfter(d);
|
3229 | if ($pos.node(d).canReplaceWith(index, index, nodeType)) {
|
3230 | return $pos.after(d + 1);
|
3231 | }
|
3232 | if (index < $pos.node(d).childCount) {
|
3233 | return null;
|
3234 | }
|
3235 | }
|
3236 | return null;
|
3237 | }
|
3238 | function createFilePlaceholder({
|
3239 | id,
|
3240 | context,
|
3241 | file,
|
3242 | pos,
|
3243 | view,
|
3244 | fileType,
|
3245 | uploadHandler
|
3246 | }) {
|
3247 | const tr = view.state.tr;
|
3248 | const insertPos = insertFilePoint(tr.doc, isNumber2(pos) ? pos : tr.selection.from, fileType);
|
3249 | if (!isNumber2(insertPos)) {
|
3250 | return;
|
3251 | }
|
3252 | const fileUploader = uploadHandler();
|
3253 | const attrs = { ...fileUploader.insert(file), id };
|
3254 | tr.insert(insertPos, fileType.createChecked(attrs));
|
3255 | const payload = { context, fileUploader };
|
3256 | setUploadPlaceholderAction(tr, { type: 0 , id, pos: insertPos, payload });
|
3257 | view.dispatch(tr);
|
3258 | return fileUploader;
|
3259 | }
|
3260 | function onFileLoaded({
|
3261 | id,
|
3262 | attrs,
|
3263 | fileType,
|
3264 | view
|
3265 | }) {
|
3266 | const placeholderPos = findUploadPlaceholderPos(view.state, id);
|
3267 | if (placeholderPos == null) {
|
3268 | return;
|
3269 | }
|
3270 | const $pos = view.state.doc.resolve(placeholderPos);
|
3271 | const fileNode = $pos.nodeAfter;
|
3272 | if (!fileNode || fileNode.type !== fileType || fileNode.attrs.id !== id) {
|
3273 | const tr2 = view.state.tr;
|
3274 | setUploadPlaceholderAction(tr2, { type: 1 , id });
|
3275 | view.dispatch(tr2);
|
3276 | return;
|
3277 | }
|
3278 | const tr = view.state.tr;
|
3279 | setUploadPlaceholderAction(tr, { type: 1 , id });
|
3280 | const fileAttrs = { ...fileNode.attrs, ...attrs, id: null };
|
3281 | tr.setNodeMarkup(placeholderPos, void 0, fileAttrs);
|
3282 | view.dispatch(tr);
|
3283 | }
|
3284 |
|
3285 |
|
3286 | var UploadExtension = class extends PlainExtension {
|
3287 | get name() {
|
3288 | return "upload";
|
3289 | }
|
3290 | createExternalPlugins() {
|
3291 | return [createUploadPlaceholderPlugin()];
|
3292 | }
|
3293 | };
|
3294 |
|
3295 |
|
3296 | function builtinPreset(options = {}) {
|
3297 | const defaultOptions2 = {
|
3298 | exitMarksOnArrowPress: KeymapExtension.defaultOptions.exitMarksOnArrowPress,
|
3299 | excludeBaseKeymap: KeymapExtension.defaultOptions.excludeBaseKeymap,
|
3300 | selectParentNodeOnEscape: KeymapExtension.defaultOptions.selectParentNodeOnEscape,
|
3301 | undoInputRuleOnBackspace: KeymapExtension.defaultOptions.undoInputRuleOnBackspace,
|
3302 | persistentSelectionClass: DecorationsExtension.defaultOptions.persistentSelectionClass
|
3303 | };
|
3304 | options = { ...defaultOptions2, ...options };
|
3305 | const keymapOptions = pick(options, [
|
3306 | "excludeBaseKeymap",
|
3307 | "selectParentNodeOnEscape",
|
3308 | "undoInputRuleOnBackspace"
|
3309 | ]);
|
3310 | const decorationsOptions = pick(options, ["persistentSelectionClass"]);
|
3311 | return [
|
3312 | new TagsExtension(),
|
3313 | new SchemaExtension(),
|
3314 | new AttributesExtension(),
|
3315 | new PluginsExtension(),
|
3316 | new InputRulesExtension(),
|
3317 | new PasteRulesExtension(),
|
3318 | new NodeViewsExtension(),
|
3319 | new SuggestExtension(),
|
3320 | new CommandsExtension(),
|
3321 | new HelpersExtension(),
|
3322 | new KeymapExtension(keymapOptions),
|
3323 | new DocChangedExtension(),
|
3324 | new UploadExtension(),
|
3325 | new DecorationsExtension(decorationsOptions)
|
3326 | ];
|
3327 | }
|
3328 |
|
3329 |
|
3330 | import { ExtensionPriority as ExtensionPriority11 } from "@remirror/core-constants";
|
3331 | import { environment as environment4 } from "@remirror/core-utils";
|
3332 | var MetaExtension = class extends PlainExtension {
|
3333 | get name() {
|
3334 | return "meta";
|
3335 | }
|
3336 | onCreate() {
|
3337 | this.store.setStoreKey("getCommandMeta", this.getCommandMeta.bind(this));
|
3338 | if (!this.options.capture) {
|
3339 | return;
|
3340 | }
|
3341 | for (const extension2 of this.store.extensions) {
|
3342 | this.captureCommands(extension2);
|
3343 | this.captureKeybindings(extension2);
|
3344 | }
|
3345 | }
|
3346 | createPlugin() {
|
3347 | return {};
|
3348 | }
|
3349 | captureCommands(extension2) {
|
3350 | var _a;
|
3351 | const decoratedCommands = (_a = extension2.decoratedCommands) != null ? _a : {};
|
3352 | const createCommands = extension2.createCommands;
|
3353 | for (const name of Object.keys(decoratedCommands)) {
|
3354 | const command2 = extension2[name];
|
3355 | extension2[name] = (...args) => (props) => {
|
3356 | var _a2;
|
3357 | const value = command2(...args)(props);
|
3358 | if (props.dispatch && value) {
|
3359 | this.setCommandMeta(props.tr, {
|
3360 | type: "command",
|
3361 | chain: props.dispatch !== ((_a2 = props.view) == null ? void 0 : _a2.dispatch),
|
3362 | name,
|
3363 | extension: extension2.name,
|
3364 | decorated: true
|
3365 | });
|
3366 | }
|
3367 | return value;
|
3368 | };
|
3369 | }
|
3370 | if (createCommands) {
|
3371 | extension2.createCommands = () => {
|
3372 | const commandsObject = createCommands();
|
3373 | for (const [name, command2] of Object.entries(commandsObject)) {
|
3374 | commandsObject[name] = (...args) => (props) => {
|
3375 | var _a2;
|
3376 | const value = command2(...args)(props);
|
3377 | if (props.dispatch && value) {
|
3378 | this.setCommandMeta(props.tr, {
|
3379 | type: "command",
|
3380 | chain: props.dispatch !== ((_a2 = props.view) == null ? void 0 : _a2.dispatch),
|
3381 | name,
|
3382 | extension: extension2.name,
|
3383 | decorated: false
|
3384 | });
|
3385 | }
|
3386 | return value;
|
3387 | };
|
3388 | }
|
3389 | return commandsObject;
|
3390 | };
|
3391 | }
|
3392 | }
|
3393 | captureKeybindings(_) {
|
3394 | }
|
3395 | getCommandMeta(tr) {
|
3396 | var _a;
|
3397 | return (_a = tr.getMeta(this.pluginKey)) != null ? _a : [];
|
3398 | }
|
3399 | setCommandMeta(tr, update) {
|
3400 | const meta = this.getCommandMeta(tr);
|
3401 | tr.setMeta(this.pluginKey, [...meta, update]);
|
3402 | }
|
3403 | };
|
3404 | MetaExtension = __decorateClass([
|
3405 | extension({
|
3406 | defaultOptions: {
|
3407 | capture: environment4.isDevelopment
|
3408 | },
|
3409 | staticKeys: ["capture"],
|
3410 | defaultPriority: ExtensionPriority11.Highest
|
3411 | })
|
3412 | ], MetaExtension);
|
3413 |
|
3414 |
|
3415 | import { createNanoEvents as createNanoEvents2 } from "nanoevents";
|
3416 | import { ErrorConstant as ErrorConstant10 } from "@remirror/core-constants";
|
3417 | import {
|
3418 | cx as cx2,
|
3419 | invariant as invariant9,
|
3420 | isEmptyArray as isEmptyArray5,
|
3421 | isFunction as isFunction6,
|
3422 | isNumber as isNumber3,
|
3423 | object as object11,
|
3424 | omitUndefined,
|
3425 | pick as pick2,
|
3426 | uniqueArray as uniqueArray3,
|
3427 | uniqueId as uniqueId3
|
3428 | } from "@remirror/core-helpers";
|
3429 | var _uid, _getProps, _previousState, _firstRender, _events, _addHandler, _initialEditorState;
|
3430 | var Framework = class {
|
3431 | constructor(options) {
|
3432 | __privateAdd(this, _uid, uniqueId3());
|
3433 | __privateAdd(this, _getProps, void 0);
|
3434 | __privateAdd(this, _previousState, void 0);
|
3435 | __publicField(this, "previousStateOverride");
|
3436 | __privateAdd(this, _firstRender, true);
|
3437 | __privateAdd(this, _events, createNanoEvents2());
|
3438 | __privateAdd(this, _addHandler, void 0);
|
3439 | __privateAdd(this, _initialEditorState, void 0);
|
3440 | __publicField(this, "getState", () => {
|
3441 | var _a;
|
3442 | return (_a = this.view.state) != null ? _a : this.initialEditorState;
|
3443 | });
|
3444 | __publicField(this, "getPreviousState", () => this.previousState);
|
3445 | __publicField(this, "dispatchTransaction", (tr) => {
|
3446 | var _a, _b, _c;
|
3447 | invariant9(!this.manager.destroyed, {
|
3448 | code: ErrorConstant10.MANAGER_PHASE_ERROR,
|
3449 | message: "A transaction was dispatched to a manager that has already been destroyed. Please check your set up, or open an issue."
|
3450 | });
|
3451 | tr = (_c = (_b = (_a = this.props).onDispatchTransaction) == null ? void 0 : _b.call(_a, tr, this.getState())) != null ? _c : tr;
|
3452 | const previousState = this.getState();
|
3453 | const { state, transactions } = previousState.applyTransaction(tr);
|
3454 | __privateSet(this, _previousState, previousState);
|
3455 | this.updateState({ state, tr, transactions });
|
3456 | const forcedUpdates = this.manager.store.getForcedUpdates(tr);
|
3457 | if (!isEmptyArray5(forcedUpdates)) {
|
3458 | this.updateViewProps(...forcedUpdates);
|
3459 | }
|
3460 | });
|
3461 | __publicField(this, "onChange", (props = object11()) => {
|
3462 | var _a, _b;
|
3463 | const onChangeProps = this.eventListenerProps(props);
|
3464 | if (__privateGet(this, _firstRender)) {
|
3465 | __privateSet(this, _firstRender, false);
|
3466 | }
|
3467 | (_b = (_a = this.props).onChange) == null ? void 0 : _b.call(_a, onChangeProps);
|
3468 | });
|
3469 | __publicField(this, "onBlur", (event) => {
|
3470 | var _a, _b;
|
3471 | const props = this.eventListenerProps();
|
3472 | (_b = (_a = this.props).onBlur) == null ? void 0 : _b.call(_a, props, event);
|
3473 | __privateGet(this, _events).emit("blur", props, event);
|
3474 | });
|
3475 | __publicField(this, "onFocus", (event) => {
|
3476 | var _a, _b;
|
3477 | const props = this.eventListenerProps();
|
3478 | (_b = (_a = this.props).onFocus) == null ? void 0 : _b.call(_a, props, event);
|
3479 | __privateGet(this, _events).emit("focus", props, event);
|
3480 | });
|
3481 | __publicField(this, "setContent", (content, { triggerChange = false } = {}) => {
|
3482 | const { doc } = this.manager.createState({ content });
|
3483 | const previousState = this.getState();
|
3484 | const { state } = this.getState().applyTransaction(previousState.tr.replaceRangeWith(0, previousState.doc.nodeSize - 2, doc));
|
3485 | if (triggerChange) {
|
3486 | return this.updateState({ state, triggerChange });
|
3487 | }
|
3488 | this.view.updateState(state);
|
3489 | });
|
3490 | __publicField(this, "clearContent", ({ triggerChange = false } = {}) => {
|
3491 | this.setContent(this.manager.createEmptyDoc(), { triggerChange });
|
3492 | });
|
3493 | __publicField(this, "createStateFromContent", (content, selection) => {
|
3494 | return this.manager.createState({ content, selection });
|
3495 | });
|
3496 | __publicField(this, "focus", (position) => {
|
3497 | this.manager.store.commands.focus(position);
|
3498 | });
|
3499 | __publicField(this, "blur", (position) => {
|
3500 | this.manager.store.commands.blur(position);
|
3501 | });
|
3502 | const { getProps, initialEditorState, element } = options;
|
3503 | __privateSet(this, _getProps, getProps);
|
3504 | __privateSet(this, _initialEditorState, initialEditorState);
|
3505 | this.manager.attachFramework(this, this.updateListener.bind(this));
|
3506 | if (this.manager.view) {
|
3507 | return;
|
3508 | }
|
3509 | const view = this.createView(initialEditorState, element);
|
3510 | this.manager.addView(view);
|
3511 | }
|
3512 | get addHandler() {
|
3513 | var _a;
|
3514 | return (_a = __privateGet(this, _addHandler)) != null ? _a : __privateSet(this, _addHandler, __privateGet(this, _events).on.bind(__privateGet(this, _events)));
|
3515 | }
|
3516 | get updatableViewProps() {
|
3517 | return {
|
3518 | attributes: () => this.getAttributes(),
|
3519 | editable: () => {
|
3520 | var _a;
|
3521 | return (_a = this.props.editable) != null ? _a : true;
|
3522 | }
|
3523 | };
|
3524 | }
|
3525 | get firstRender() {
|
3526 | return __privateGet(this, _firstRender);
|
3527 | }
|
3528 | get props() {
|
3529 | return __privateGet(this, _getProps).call(this);
|
3530 | }
|
3531 | get previousState() {
|
3532 | var _a, _b;
|
3533 | return (_b = (_a = this.previousStateOverride) != null ? _a : __privateGet(this, _previousState)) != null ? _b : this.initialEditorState;
|
3534 | }
|
3535 | get manager() {
|
3536 | return this.props.manager;
|
3537 | }
|
3538 | get view() {
|
3539 | return this.manager.view;
|
3540 | }
|
3541 | get uid() {
|
3542 | return __privateGet(this, _uid);
|
3543 | }
|
3544 | get initialEditorState() {
|
3545 | return __privateGet(this, _initialEditorState);
|
3546 | }
|
3547 | updateListener(props) {
|
3548 | const { state, tr } = props;
|
3549 | return __privateGet(this, _events).emit("updated", this.eventListenerProps({ state, tr }));
|
3550 | }
|
3551 | update(options) {
|
3552 | const { getProps } = options;
|
3553 | __privateSet(this, _getProps, getProps);
|
3554 | return this;
|
3555 | }
|
3556 | updateViewProps(...keys3) {
|
3557 | const props = pick2(this.updatableViewProps, keys3);
|
3558 | this.view.setProps({ ...this.view.props, ...props });
|
3559 | }
|
3560 | getAttributes(ssr) {
|
3561 | var _a;
|
3562 | const { attributes, autoFocus, classNames = [], label, editable } = this.props;
|
3563 | const managerAttributes = (_a = this.manager.store) == null ? void 0 : _a.attributes;
|
3564 | const propAttributes = isFunction6(attributes) ? attributes(this.eventListenerProps()) : attributes;
|
3565 | let focus = {};
|
3566 | if (autoFocus || isNumber3(autoFocus)) {
|
3567 | focus = ssr ? { autoFocus: true } : { autofocus: "true" };
|
3568 | }
|
3569 | const uniqueClasses = uniqueArray3(cx2(ssr && "Prosemirror", "remirror-editor", managerAttributes == null ? void 0 : managerAttributes.class, ...classNames).split(" ")).join(" ");
|
3570 | const defaultAttributes = {
|
3571 | role: "textbox",
|
3572 | ...focus,
|
3573 | "aria-multiline": "true",
|
3574 | ...!(editable != null ? editable : true) ? { "aria-readonly": "true" } : {},
|
3575 | "aria-label": label != null ? label : "",
|
3576 | ...managerAttributes,
|
3577 | class: uniqueClasses
|
3578 | };
|
3579 | return omitUndefined({ ...defaultAttributes, ...propAttributes });
|
3580 | }
|
3581 | addFocusListeners() {
|
3582 | this.view.dom.addEventListener("blur", this.onBlur);
|
3583 | this.view.dom.addEventListener("focus", this.onFocus);
|
3584 | }
|
3585 | removeFocusListeners() {
|
3586 | this.view.dom.removeEventListener("blur", this.onBlur);
|
3587 | this.view.dom.removeEventListener("focus", this.onFocus);
|
3588 | }
|
3589 | destroy() {
|
3590 | __privateGet(this, _events).emit("destroy");
|
3591 | if (this.view) {
|
3592 | this.removeFocusListeners();
|
3593 | }
|
3594 | }
|
3595 | eventListenerProps(props = object11()) {
|
3596 | const { state, tr, transactions } = props;
|
3597 | return {
|
3598 | tr,
|
3599 | transactions,
|
3600 | internalUpdate: !tr,
|
3601 | view: this.view,
|
3602 | firstRender: __privateGet(this, _firstRender),
|
3603 | state: state != null ? state : this.getState(),
|
3604 | createStateFromContent: this.createStateFromContent,
|
3605 | previousState: this.previousState,
|
3606 | helpers: this.manager.store.helpers
|
3607 | };
|
3608 | }
|
3609 | get baseOutput() {
|
3610 | return {
|
3611 | manager: this.manager,
|
3612 | ...this.manager.store,
|
3613 | addHandler: this.addHandler,
|
3614 | focus: this.focus,
|
3615 | blur: this.blur,
|
3616 | uid: __privateGet(this, _uid),
|
3617 | view: this.view,
|
3618 | getState: this.getState,
|
3619 | getPreviousState: this.getPreviousState,
|
3620 | getExtension: this.manager.getExtension.bind(this.manager),
|
3621 | hasExtension: this.manager.hasExtension.bind(this.manager),
|
3622 | clearContent: this.clearContent,
|
3623 | setContent: this.setContent
|
3624 | };
|
3625 | }
|
3626 | };
|
3627 | _uid = new WeakMap();
|
3628 | _getProps = new WeakMap();
|
3629 | _previousState = new WeakMap();
|
3630 | _firstRender = new WeakMap();
|
3631 | _events = new WeakMap();
|
3632 | _addHandler = new WeakMap();
|
3633 | _initialEditorState = new WeakMap();
|
3634 |
|
3635 |
|
3636 | import { createNanoEvents as createNanoEvents3 } from "nanoevents";
|
3637 | import {
|
3638 | __INTERNAL_REMIRROR_IDENTIFIER_KEY__ as __INTERNAL_REMIRROR_IDENTIFIER_KEY__3,
|
3639 | ErrorConstant as ErrorConstant12,
|
3640 | ManagerPhase as ManagerPhase2,
|
3641 | RemirrorIdentifier as RemirrorIdentifier3
|
3642 | } from "@remirror/core-constants";
|
3643 | import {
|
3644 | freeze as freeze3,
|
3645 | getLazyArray,
|
3646 | includes as includes4,
|
3647 | invariant as invariant11,
|
3648 | isNullOrUndefined as isNullOrUndefined2,
|
3649 | isString as isString6,
|
3650 | object as object12
|
3651 | } from "@remirror/core-helpers";
|
3652 | import {
|
3653 | createDocumentNode,
|
3654 | getDocument,
|
3655 | getTextSelection as getTextSelection3,
|
3656 | isIdentifierOfType as isIdentifierOfType2,
|
3657 | isRemirrorType as isRemirrorType2
|
3658 | } from "@remirror/core-utils";
|
3659 | import { EditorState as EditorState3 } from "@remirror/pm/state";
|
3660 |
|
3661 |
|
3662 | import warning from "tiny-warning";
|
3663 | import { ErrorConstant as ErrorConstant11 } from "@remirror/core-constants";
|
3664 | import { invariant as invariant10, isEmptyArray as isEmptyArray6, sort as sort3 } from "@remirror/core-helpers";
|
3665 | function transformExtensions(initialExtensions, settings) {
|
3666 | const extensions = [];
|
3667 | const extensionMap = new WeakMap();
|
3668 | const parentExtensions = [];
|
3669 | const duplicateMap = new WeakMap();
|
3670 | let gatheredExtensions = [];
|
3671 | const gatherRawExtensionConfig = { duplicateMap, parentExtensions, gatheredExtensions, settings };
|
3672 | for (const extension2 of initialExtensions) {
|
3673 | gatherRawExtensions(gatherRawExtensionConfig, { extension: extension2 });
|
3674 | }
|
3675 | gatheredExtensions = sort3(gatheredExtensions, (a, z) => z.priority - a.priority);
|
3676 | const found = new WeakSet();
|
3677 | const names = new Set();
|
3678 | for (const extension2 of gatheredExtensions) {
|
3679 | const key2 = extension2.constructor;
|
3680 | const name = extension2.name;
|
3681 | const duplicates = duplicateMap.get(key2);
|
3682 | invariant10(duplicates, {
|
3683 | message: `No entries were found for the ExtensionConstructor ${extension2.name}`,
|
3684 | code: ErrorConstant11.INTERNAL
|
3685 | });
|
3686 | if (found.has(key2) || names.has(name)) {
|
3687 | continue;
|
3688 | }
|
3689 | found.add(key2);
|
3690 | names.add(name);
|
3691 | extensions.push(extension2);
|
3692 | extensionMap.set(key2, extension2);
|
3693 | duplicates.forEach((parent) => parent == null ? void 0 : parent.replaceChildExtension(key2, extension2));
|
3694 | }
|
3695 | const missing = [];
|
3696 | for (const extension2 of extensions) {
|
3697 | findMissingExtensions({ extension: extension2, found, missing });
|
3698 | }
|
3699 | invariant10(isEmptyArray6(missing), {
|
3700 | code: ErrorConstant11.MISSING_REQUIRED_EXTENSION,
|
3701 | message: missing.map(({ Constructor, extension: extension2 }) => `The extension '${extension2.name}' requires '${Constructor.name} in order to run correctly.`).join("\n")
|
3702 | });
|
3703 | return { extensions, extensionMap };
|
3704 | }
|
3705 | function gatherRawExtensions(config, props) {
|
3706 | var _a;
|
3707 | const { gatheredExtensions, duplicateMap, parentExtensions, settings } = config;
|
3708 | const { extension: extension2, parentExtension } = props;
|
3709 | let { names = [] } = props;
|
3710 | invariant10(isExtension(extension2), {
|
3711 | code: ErrorConstant11.INVALID_MANAGER_EXTENSION,
|
3712 | message: `An invalid extension: ${extension2} was provided to the [[\`RemirrorManager\`]].`
|
3713 | });
|
3714 | const childExtensions = extension2.extensions;
|
3715 | extension2.setPriority((_a = settings.priority) == null ? void 0 : _a[extension2.name]);
|
3716 | gatheredExtensions.push(extension2);
|
3717 | updateExtensionDuplicates({ duplicateMap, extension: extension2, parentExtension });
|
3718 | if (childExtensions.length === 0) {
|
3719 | return;
|
3720 | }
|
3721 | if (names.includes(extension2.name)) {
|
3722 | warning(false, `Circular dependency encountered when loading extensions: ${names.join(" > ")} > ${extension2.name}`);
|
3723 | return;
|
3724 | }
|
3725 | names = [...names, extension2.name];
|
3726 | parentExtensions.push(extension2);
|
3727 | for (const child of childExtensions) {
|
3728 | gatherRawExtensions(config, { names, extension: child, parentExtension: extension2 });
|
3729 | }
|
3730 | }
|
3731 | function findMissingExtensions(props) {
|
3732 | var _a;
|
3733 | const { extension: extension2, found, missing } = props;
|
3734 | if (!extension2.requiredExtensions) {
|
3735 | return;
|
3736 | }
|
3737 | for (const Constructor of (_a = extension2.requiredExtensions) != null ? _a : []) {
|
3738 | if (found.has(Constructor)) {
|
3739 | continue;
|
3740 | }
|
3741 | missing.push({ Constructor, extension: extension2 });
|
3742 | }
|
3743 | }
|
3744 | function updateExtensionDuplicates(props) {
|
3745 | const { duplicateMap, extension: extension2, parentExtension } = props;
|
3746 | const key2 = extension2.constructor;
|
3747 | const duplicate = duplicateMap.get(key2);
|
3748 | const parentToAdd = parentExtension ? [parentExtension] : [];
|
3749 | duplicateMap.set(key2, duplicate ? [...duplicate, ...parentToAdd] : parentToAdd);
|
3750 | }
|
3751 | function extractLifecycleMethods(props) {
|
3752 | var _a, _b, _c, _d;
|
3753 | const { extension: extension2, nodeNames, markNames, plainNames, store, handlers } = props;
|
3754 | extension2.setStore(store);
|
3755 | const createHandler = (_a = extension2.onCreate) == null ? void 0 : _a.bind(extension2);
|
3756 | const viewHandler = (_b = extension2.onView) == null ? void 0 : _b.bind(extension2);
|
3757 | const stateUpdateHandler = (_c = extension2.onStateUpdate) == null ? void 0 : _c.bind(extension2);
|
3758 | const destroyHandler = (_d = extension2.onDestroy) == null ? void 0 : _d.bind(extension2);
|
3759 | if (createHandler) {
|
3760 | handlers.create.push(createHandler);
|
3761 | }
|
3762 | if (viewHandler) {
|
3763 | handlers.view.push(viewHandler);
|
3764 | }
|
3765 | if (stateUpdateHandler) {
|
3766 | handlers.update.push(stateUpdateHandler);
|
3767 | }
|
3768 | if (destroyHandler) {
|
3769 | handlers.destroy.push(destroyHandler);
|
3770 | }
|
3771 | if (isMarkExtension(extension2)) {
|
3772 | markNames.push(extension2.name);
|
3773 | }
|
3774 | if (isNodeExtension(extension2) && extension2.name !== "doc") {
|
3775 | nodeNames.push(extension2.name);
|
3776 | }
|
3777 | if (isPlainExtension(extension2)) {
|
3778 | plainNames.push(extension2.name);
|
3779 | }
|
3780 | }
|
3781 |
|
3782 |
|
3783 | var _extensionStore, _stringHandlers, _store, _extensions, _extensionMap, _phase, _settings, _firstStateUpdate, _handlers, _disposers, _events2, _framework, _disposeFramework;
|
3784 | var _RemirrorManager = class {
|
3785 | constructor(initialExtension, settings = {}) {
|
3786 | __privateAdd(this, _extensionStore, void 0);
|
3787 | __privateAdd(this, _stringHandlers, object12());
|
3788 | __privateAdd(this, _store, object12());
|
3789 | __privateAdd(this, _extensions, void 0);
|
3790 | __privateAdd(this, _extensionMap, void 0);
|
3791 | __privateAdd(this, _phase, ManagerPhase2.None);
|
3792 | __privateAdd(this, _settings, void 0);
|
3793 | __privateAdd(this, _firstStateUpdate, true);
|
3794 | __privateAdd(this, _handlers, {
|
3795 | create: [],
|
3796 | view: [],
|
3797 | update: [],
|
3798 | destroy: []
|
3799 | });
|
3800 | __privateAdd(this, _disposers, []);
|
3801 | __privateAdd(this, _events2, createNanoEvents3());
|
3802 | __privateAdd(this, _framework, void 0);
|
3803 | __privateAdd(this, _disposeFramework, void 0);
|
3804 | __publicField(this, "getState", () => {
|
3805 | var _a;
|
3806 | if (__privateGet(this, _phase) >= ManagerPhase2.EditorView) {
|
3807 | return this.view.state;
|
3808 | }
|
3809 | invariant11(__privateGet(this, _framework), {
|
3810 | code: ErrorConstant12.MANAGER_PHASE_ERROR,
|
3811 | message: "`getState` can only be called after the `Framework` or the `EditorView` has been added to the manager`. Check your plugins to make sure that the decorations callback uses the state argument."
|
3812 | });
|
3813 | return (_a = __privateGet(this, _framework)) == null ? void 0 : _a.initialEditorState;
|
3814 | });
|
3815 | __publicField(this, "updateState", (state) => {
|
3816 | const previousState = this.getState();
|
3817 | this.view.updateState(state);
|
3818 | this.onStateUpdate({ previousState, state });
|
3819 | });
|
3820 | const { extensions, extensionMap } = transformExtensions(initialExtension, settings);
|
3821 | __privateSet(this, _settings, settings);
|
3822 | __privateSet(this, _extensions, freeze3(extensions));
|
3823 | __privateSet(this, _extensionMap, extensionMap);
|
3824 | __privateSet(this, _extensionStore, this.createExtensionStore());
|
3825 | __privateSet(this, _phase, ManagerPhase2.Create);
|
3826 | this.setupLifecycleHandlers();
|
3827 | for (const handler of __privateGet(this, _handlers).create) {
|
3828 | const disposer = handler();
|
3829 | if (disposer) {
|
3830 | __privateGet(this, _disposers).push(disposer);
|
3831 | }
|
3832 | }
|
3833 | }
|
3834 | static create(extensions, settings = {}) {
|
3835 | return new _RemirrorManager([...getLazyArray(extensions), ...builtinPreset(settings.builtin)], settings);
|
3836 | }
|
3837 | get [__INTERNAL_REMIRROR_IDENTIFIER_KEY__3]() {
|
3838 | return RemirrorIdentifier3.Manager;
|
3839 | }
|
3840 | get destroyed() {
|
3841 | return __privateGet(this, _phase) === ManagerPhase2.Destroy;
|
3842 | }
|
3843 | get mounted() {
|
3844 | return __privateGet(this, _phase) >= ManagerPhase2.EditorView && __privateGet(this, _phase) < ManagerPhase2.Destroy;
|
3845 | }
|
3846 | get output() {
|
3847 | var _a;
|
3848 | return (_a = __privateGet(this, _framework)) == null ? void 0 : _a.frameworkOutput;
|
3849 | }
|
3850 | get frameworkAttached() {
|
3851 | return !!__privateGet(this, _framework);
|
3852 | }
|
3853 | get extensions() {
|
3854 | return __privateGet(this, _extensions);
|
3855 | }
|
3856 | get stringHandlers() {
|
3857 | return __privateGet(this, _stringHandlers);
|
3858 | }
|
3859 | get store() {
|
3860 | return freeze3(__privateGet(this, _store));
|
3861 | }
|
3862 | get extensionStore() {
|
3863 | return freeze3(__privateGet(this, _extensionStore));
|
3864 | }
|
3865 | get tr() {
|
3866 | return this.getExtension(CommandsExtension).transaction;
|
3867 | }
|
3868 | get nodes() {
|
3869 | return __privateGet(this, _store).nodes;
|
3870 | }
|
3871 | get marks() {
|
3872 | return __privateGet(this, _store).marks;
|
3873 | }
|
3874 | get schema() {
|
3875 | return __privateGet(this, _store).schema;
|
3876 | }
|
3877 | get extensionTags() {
|
3878 | return __privateGet(this, _store).tags;
|
3879 | }
|
3880 | get view() {
|
3881 | return __privateGet(this, _store).view;
|
3882 | }
|
3883 | get settings() {
|
3884 | return __privateGet(this, _settings);
|
3885 | }
|
3886 | get document() {
|
3887 | var _a;
|
3888 | return (_a = __privateGet(this, _settings).document) != null ? _a : getDocument();
|
3889 | }
|
3890 | setupLifecycleHandlers() {
|
3891 | const store = __privateGet(this, _extensionStore);
|
3892 | const handlers = __privateGet(this, _handlers);
|
3893 | const nodeNames = [];
|
3894 | const markNames = [];
|
3895 | const plainNames = [];
|
3896 | store.nodeNames = nodeNames;
|
3897 | store.markNames = markNames;
|
3898 | store.plainNames = plainNames;
|
3899 | for (const extension2 of __privateGet(this, _extensions)) {
|
3900 | extractLifecycleMethods({ extension: extension2, nodeNames, markNames, plainNames, handlers, store });
|
3901 | }
|
3902 | }
|
3903 | setStringHandler(name, handler) {
|
3904 | __privateGet(this, _stringHandlers)[name] = handler;
|
3905 | }
|
3906 | setStoreKey(key2, value) {
|
3907 | __privateGet(this, _store)[key2] = value;
|
3908 | }
|
3909 | getStoreKey(key2) {
|
3910 | const value = __privateGet(this, _store)[key2];
|
3911 | invariant11(!isNullOrUndefined2(value), {
|
3912 | code: ErrorConstant12.MANAGER_PHASE_ERROR,
|
3913 | message: "`getStoreKey` should not be called before the values are available."
|
3914 | });
|
3915 | return value;
|
3916 | }
|
3917 | setExtensionStore(key2, value) {
|
3918 | invariant11(__privateGet(this, _phase) <= ManagerPhase2.EditorView, {
|
3919 | code: ErrorConstant12.MANAGER_PHASE_ERROR,
|
3920 | message: "`setExtensionStore` should only be called during the `onCreate` lifecycle hook. Make sure to only call it within the returned methods."
|
3921 | });
|
3922 | __privateGet(this, _extensionStore)[key2] = value;
|
3923 | }
|
3924 | createExtensionStore() {
|
3925 | const store = object12();
|
3926 | const enumerable = true;
|
3927 | let currentState;
|
3928 | let previousState;
|
3929 | Object.defineProperties(store, {
|
3930 | extensions: { get: () => __privateGet(this, _extensions), enumerable },
|
3931 | phase: { get: () => __privateGet(this, _phase), enumerable },
|
3932 | view: { get: () => this.view, enumerable },
|
3933 | managerSettings: { get: () => freeze3(__privateGet(this, _settings)), enumerable },
|
3934 | getState: { value: this.getState, enumerable },
|
3935 | updateState: { value: this.updateState, enumerable },
|
3936 | isMounted: { value: () => this.mounted, enumerable },
|
3937 | getExtension: { value: this.getExtension.bind(this), enumerable },
|
3938 | manager: { get: () => this, enumerable },
|
3939 | document: { get: () => this.document, enumerable },
|
3940 | stringHandlers: { get: () => __privateGet(this, _stringHandlers), enumerable },
|
3941 | currentState: {
|
3942 | get: () => currentState != null ? currentState : currentState = this.getState(),
|
3943 | set: (state) => {
|
3944 | currentState = state;
|
3945 | },
|
3946 | enumerable
|
3947 | },
|
3948 | previousState: {
|
3949 | get: () => previousState,
|
3950 | set: (state) => {
|
3951 | previousState = state;
|
3952 | },
|
3953 | enumerable
|
3954 | }
|
3955 | });
|
3956 | store.getStoreKey = this.getStoreKey.bind(this);
|
3957 | store.setStoreKey = this.setStoreKey.bind(this);
|
3958 | store.setExtensionStore = this.setExtensionStore.bind(this);
|
3959 | store.setStringHandler = this.setStringHandler.bind(this);
|
3960 | return store;
|
3961 | }
|
3962 | addView(view) {
|
3963 | if (__privateGet(this, _phase) >= ManagerPhase2.EditorView) {
|
3964 | return this;
|
3965 | }
|
3966 | __privateSet(this, _firstStateUpdate, true);
|
3967 | __privateSet(this, _phase, ManagerPhase2.EditorView);
|
3968 | __privateGet(this, _store).view = view;
|
3969 | for (const handler of __privateGet(this, _handlers).view) {
|
3970 | const disposer = handler(view);
|
3971 | if (disposer) {
|
3972 | __privateGet(this, _disposers).push(disposer);
|
3973 | }
|
3974 | }
|
3975 | return this;
|
3976 | }
|
3977 | attachFramework(framework, updateHandler) {
|
3978 | var _a;
|
3979 | if (__privateGet(this, _framework) === framework) {
|
3980 | return;
|
3981 | }
|
3982 | if (__privateGet(this, _framework)) {
|
3983 | __privateGet(this, _framework).destroy();
|
3984 | (_a = __privateGet(this, _disposeFramework)) == null ? void 0 : _a.call(this);
|
3985 | }
|
3986 | __privateSet(this, _framework, framework);
|
3987 | __privateSet(this, _disposeFramework, this.addHandler("stateUpdate", updateHandler));
|
3988 | }
|
3989 | createEmptyDoc() {
|
3990 | var _a;
|
3991 | const doc = (_a = this.schema.nodes.doc) == null ? void 0 : _a.createAndFill();
|
3992 | invariant11(doc, {
|
3993 | code: ErrorConstant12.INVALID_CONTENT,
|
3994 | message: `An empty node could not be created due to an invalid schema.`
|
3995 | });
|
3996 | return doc;
|
3997 | }
|
3998 | createState(props = {}) {
|
3999 | const { onError, defaultSelection = "end" } = this.settings;
|
4000 | const {
|
4001 | content = this.createEmptyDoc(),
|
4002 | selection = defaultSelection,
|
4003 | stringHandler = this.settings.stringHandler
|
4004 | } = props;
|
4005 | const { schema, plugins } = this.store;
|
4006 | const doc = createDocumentNode({
|
4007 | stringHandler: isString6(stringHandler) ? this.stringHandlers[stringHandler] : stringHandler,
|
4008 | document: this.document,
|
4009 | content,
|
4010 | onError,
|
4011 | schema,
|
4012 | selection
|
4013 | });
|
4014 | return EditorState3.create({
|
4015 | schema,
|
4016 | doc,
|
4017 | plugins,
|
4018 | selection: getTextSelection3(selection, doc)
|
4019 | });
|
4020 | }
|
4021 | addHandler(event, cb) {
|
4022 | return __privateGet(this, _events2).on(event, cb);
|
4023 | }
|
4024 | onStateUpdate(props) {
|
4025 | const firstUpdate = __privateGet(this, _firstStateUpdate);
|
4026 | __privateGet(this, _extensionStore).currentState = props.state;
|
4027 | __privateGet(this, _extensionStore).previousState = props.previousState;
|
4028 | if (firstUpdate) {
|
4029 | __privateSet(this, _phase, ManagerPhase2.Runtime);
|
4030 | __privateSet(this, _firstStateUpdate, false);
|
4031 | }
|
4032 | const propsWithUpdate = { ...props, firstUpdate };
|
4033 | for (const handler of __privateGet(this, _handlers).update) {
|
4034 | handler(propsWithUpdate);
|
4035 | }
|
4036 | __privateGet(this, _events2).emit("stateUpdate", propsWithUpdate);
|
4037 | }
|
4038 | getExtension(Constructor) {
|
4039 | const extension2 = __privateGet(this, _extensionMap).get(Constructor);
|
4040 | invariant11(extension2, {
|
4041 | code: ErrorConstant12.INVALID_MANAGER_EXTENSION,
|
4042 | message: `'${Constructor.name}' doesn't exist within this manager. Make sure it is properly added before attempting to use it.`
|
4043 | });
|
4044 | return extension2;
|
4045 | }
|
4046 | hasExtension(Constructor) {
|
4047 | const extension2 = __privateGet(this, _extensionMap).get(Constructor);
|
4048 | return !!extension2;
|
4049 | }
|
4050 | clone() {
|
4051 | const extensions = __privateGet(this, _extensions).map((e) => e.clone(e.options));
|
4052 | const manager = _RemirrorManager.create(() => extensions, __privateGet(this, _settings));
|
4053 | __privateGet(this, _events2).emit("clone", manager);
|
4054 | return manager;
|
4055 | }
|
4056 | recreate(extensions = [], settings = {}) {
|
4057 | const currentExtensions = __privateGet(this, _extensions).map((e) => e.clone(e.initialOptions));
|
4058 | const manager = _RemirrorManager.create(() => [...currentExtensions, ...extensions], settings);
|
4059 | __privateGet(this, _events2).emit("recreate", manager);
|
4060 | return manager;
|
4061 | }
|
4062 | destroy() {
|
4063 | var _a, _b, _c, _d, _e, _f, _g;
|
4064 | __privateSet(this, _phase, ManagerPhase2.Destroy);
|
4065 | for (const plugin of (_b = (_a = this.view) == null ? void 0 : _a.state.plugins) != null ? _b : []) {
|
4066 | (_d = (_c = plugin.getState(this.view.state)) == null ? void 0 : _c.destroy) == null ? void 0 : _d.call(_c);
|
4067 | }
|
4068 | (_e = __privateGet(this, _framework)) == null ? void 0 : _e.destroy();
|
4069 | (_f = __privateGet(this, _disposeFramework)) == null ? void 0 : _f.call(this);
|
4070 | for (const dispose of __privateGet(this, _disposers)) {
|
4071 | dispose();
|
4072 | }
|
4073 | for (const onDestroy of __privateGet(this, _handlers).destroy) {
|
4074 | onDestroy();
|
4075 | }
|
4076 | (_g = this.view) == null ? void 0 : _g.destroy();
|
4077 | __privateGet(this, _events2).emit("destroy");
|
4078 | }
|
4079 | includes(mustIncludeList) {
|
4080 | const names = [];
|
4081 | const extensionsAndPresets = [];
|
4082 | for (const item of __privateGet(this, _extensions)) {
|
4083 | names.push(item.name, item.constructorName);
|
4084 | extensionsAndPresets.push(item.constructor);
|
4085 | }
|
4086 | return mustIncludeList.every((item) => isString6(item) ? includes4(names, item) : includes4(extensionsAndPresets, item));
|
4087 | }
|
4088 | };
|
4089 | var RemirrorManager = _RemirrorManager;
|
4090 | _extensionStore = new WeakMap();
|
4091 | _stringHandlers = new WeakMap();
|
4092 | _store = new WeakMap();
|
4093 | _extensions = new WeakMap();
|
4094 | _extensionMap = new WeakMap();
|
4095 | _phase = new WeakMap();
|
4096 | _settings = new WeakMap();
|
4097 | _firstStateUpdate = new WeakMap();
|
4098 | _handlers = new WeakMap();
|
4099 | _disposers = new WeakMap();
|
4100 | _events2 = new WeakMap();
|
4101 | _framework = new WeakMap();
|
4102 | _disposeFramework = new WeakMap();
|
4103 | function isRemirrorManager(value, mustIncludeList) {
|
4104 | if (!isRemirrorType2(value) || !isIdentifierOfType2(value, RemirrorIdentifier3.Manager)) {
|
4105 | return false;
|
4106 | }
|
4107 | if (!mustIncludeList) {
|
4108 | return true;
|
4109 | }
|
4110 | return value.includes(mustIncludeList);
|
4111 | }
|
4112 |
|
4113 |
|
4114 | export * from "@remirror/core-constants";
|
4115 | export * from "@remirror/core-helpers";
|
4116 | export * from "@remirror/core-types";
|
4117 | export * from "@remirror/core-utils";
|
4118 | export {
|
4119 | AttributesExtension,
|
4120 | CommandsExtension,
|
4121 | DEFAULT_SHORTCUTS,
|
4122 | DecorationsExtension,
|
4123 | DelayedCommand,
|
4124 | DocChangedExtension,
|
4125 | Framework,
|
4126 | GOOGLE_DOC_SHORTCUTS,
|
4127 | HelpersExtension,
|
4128 | InputRulesExtension,
|
4129 | KeymapExtension,
|
4130 | MarkExtension,
|
4131 | MetaExtension,
|
4132 | NodeExtension,
|
4133 | NodeViewsExtension,
|
4134 | PasteRulesExtension,
|
4135 | PlainExtension,
|
4136 | PluginsExtension,
|
4137 | RemirrorManager,
|
4138 | SchemaExtension,
|
4139 | SuggestExtension,
|
4140 | TagsExtension,
|
4141 | UploadExtension,
|
4142 | builtinPreset,
|
4143 | command,
|
4144 | delayedCommand,
|
4145 | extension,
|
4146 | extensionDecorator,
|
4147 | findUploadPlaceholderPayload,
|
4148 | findUploadPlaceholderPos,
|
4149 | hasUploadingFile,
|
4150 | helper,
|
4151 | insertText,
|
4152 | isDelayedValue,
|
4153 | isExtension,
|
4154 | isExtensionConstructor,
|
4155 | isExtensionTag,
|
4156 | isMarkExtension,
|
4157 | isNodeExtension,
|
4158 | isPlainExtension,
|
4159 | isRemirrorManager,
|
4160 | keyBinding,
|
4161 | keyboardShortcuts,
|
4162 | mutateDefaultExtensionOptions,
|
4163 | setUploadPlaceholderAction,
|
4164 | toggleMark,
|
4165 | uploadFile
|
4166 | };
|
4167 |
|