@acorex/cdk 22.4.45 → 22.4.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import { isPlatformBrowser } from '@angular/common';
2
2
  import * as i0 from '@angular/core';
3
- import { signal, inject, PLATFORM_ID, Injectable } from '@angular/core';
3
+ import { signal, inject, PLATFORM_ID, NgZone, Injectable } from '@angular/core';
4
4
 
5
5
  const AX_TEXT_EDITOR_FONT_SIZE_TOKEN_VALUES = {
6
6
  small: '0.75em',
@@ -45,6 +45,7 @@ const AX_EMPTY_TEXT_EDITOR_STATE = {
45
45
  italic: false,
46
46
  underline: false,
47
47
  strike: false,
48
+ spoiler: false,
48
49
  code: false,
49
50
  codeBlock: false,
50
51
  headingLevel: null,
@@ -66,6 +67,533 @@ const AX_EMPTY_TEXT_EDITOR_STATE = {
66
67
  isFocused: false,
67
68
  };
68
69
 
70
+ const DEFAULT_MENTION_LIMIT = 8;
71
+ /** Text before the caret. `start` is the index of the trigger inside `text`. */
72
+ function findTextEditorMentionQuery(text, char = '@') {
73
+ const trigger = mentionTrigger(char);
74
+ const escaped = escapeRegExp(trigger);
75
+ const match = new RegExp(`(?:^|[\\s\\u00a0\\ufffc])(${escaped}([^\\s${escapeCharClass(trigger)}]*))$`, 'u').exec(text);
76
+ if (!match?.[1])
77
+ return null;
78
+ return { start: text.length - match[1].length, query: match[2] ?? '' };
79
+ }
80
+ /** Local filter used when `mentions` is an array. */
81
+ function filterTextEditorMentionItems(items, query, limit = DEFAULT_MENTION_LIMIT) {
82
+ const needle = query.trim().toLocaleLowerCase();
83
+ const matched = needle
84
+ ? items.filter((item) => item.label.toLocaleLowerCase().includes(needle) || item.id.toLocaleLowerCase().includes(needle))
85
+ : [...items];
86
+ return matched.slice(0, Math.max(0, limit));
87
+ }
88
+ function isMentionOptions(source) {
89
+ return typeof source === 'object' && source !== null && !Array.isArray(source);
90
+ }
91
+ /** Normalizes the `mentions` input into a suggestion source. `null` disables the picker. */
92
+ function resolveTextEditorMentions(source) {
93
+ if (source == null)
94
+ return null;
95
+ if (typeof source === 'function')
96
+ return { items: source };
97
+ if (Array.isArray(source)) {
98
+ const items = source;
99
+ return { items: (query) => filterTextEditorMentionItems(items, query) };
100
+ }
101
+ if (isMentionOptions(source)) {
102
+ const items = source.items;
103
+ const limit = source.limit ?? DEFAULT_MENTION_LIMIT;
104
+ return {
105
+ char: source.char,
106
+ limit: source.limit,
107
+ items: typeof items === 'function' ? items : (query) => filterTextEditorMentionItems(items, query, limit),
108
+ };
109
+ }
110
+ return null;
111
+ }
112
+ /** Keeps only URLs that are safe to put in an `<img src>`. */
113
+ function textEditorMentionAvatar(value) {
114
+ if (!value)
115
+ return null;
116
+ const src = value.trim();
117
+ if (!src || /^javascript:/i.test(src))
118
+ return null;
119
+ if (/^(https?:|blob:|data:image\/)|^\//i.test(src))
120
+ return src;
121
+ return null;
122
+ }
123
+ function mentionSuggestionsEqual(left, right) {
124
+ if (left === right)
125
+ return true;
126
+ if (!left || !right)
127
+ return false;
128
+ if (left.query !== right.query || left.index !== right.index || left.loading !== right.loading)
129
+ return false;
130
+ if (left.items.length !== right.items.length)
131
+ return false;
132
+ if (left.rect?.top !== right.rect?.top ||
133
+ left.rect?.left !== right.rect?.left ||
134
+ left.rect?.bottom !== right.rect?.bottom) {
135
+ return false;
136
+ }
137
+ return left.items.every((item, index) => item.id === right.items[index]?.id && item.label === right.items[index]?.label);
138
+ }
139
+ function createTextEditorMentionExtension(runtime, controller) {
140
+ const { core, pmState, pmView } = runtime;
141
+ const key = new pmState.PluginKey('axTextEditorMention');
142
+ let currentView = null;
143
+ let alive = false;
144
+ let composing = false;
145
+ let requestSerial = 0;
146
+ const emptyDecorations = (state) => pmView.DecorationSet.empty;
147
+ const inactive = (state, dismissed = null) => ({
148
+ active: false,
149
+ range: null,
150
+ query: '',
151
+ items: [],
152
+ index: 0,
153
+ loading: false,
154
+ request: 0,
155
+ dismissed,
156
+ decorations: emptyDecorations(state),
157
+ });
158
+ let plugin;
159
+ const pendingRequests = new Set();
160
+ const finishRequest = (requestId, items, attempt = 0) => {
161
+ const view = currentView;
162
+ if (!view || !alive) {
163
+ pendingRequests.delete(requestId);
164
+ return;
165
+ }
166
+ const pluginState = key.getState(view.state);
167
+ if (!pluginState?.active || pluginState.request !== requestId) {
168
+ pendingRequests.delete(requestId);
169
+ return;
170
+ }
171
+ try {
172
+ view.dispatch(view.state.tr.setMeta(key, { type: 'items', request: requestId, items }));
173
+ pendingRequests.delete(requestId);
174
+ }
175
+ catch {
176
+ if (attempt < 5)
177
+ queueMicrotask(() => finishRequest(requestId, items, attempt + 1));
178
+ else
179
+ pendingRequests.delete(requestId);
180
+ }
181
+ };
182
+ const scheduleRequest = (requestId, query, limit) => {
183
+ if (pendingRequests.has(requestId))
184
+ return;
185
+ pendingRequests.add(requestId);
186
+ queueMicrotask(() => {
187
+ const view = currentView;
188
+ if (!view || !alive) {
189
+ pendingRequests.delete(requestId);
190
+ return;
191
+ }
192
+ void Promise.resolve()
193
+ .then(() => controller.getSource()?.items(query) ?? [])
194
+ .then(async (items) => {
195
+ const resolved = await items;
196
+ if (requestId !== requestSerial) {
197
+ pendingRequests.delete(requestId);
198
+ return;
199
+ }
200
+ finishRequest(requestId, (Array.isArray(resolved) ? resolved : []).slice(0, Math.max(0, limit)));
201
+ })
202
+ .catch(() => {
203
+ if (requestId !== requestSerial) {
204
+ pendingRequests.delete(requestId);
205
+ return;
206
+ }
207
+ finishRequest(requestId, []);
208
+ });
209
+ });
210
+ };
211
+ const node = core.Node.create({
212
+ name: 'mention',
213
+ group: 'inline',
214
+ inline: true,
215
+ atom: true,
216
+ selectable: false,
217
+ draggable: false,
218
+ addAttributes() {
219
+ return {
220
+ id: {
221
+ default: null,
222
+ parseHTML: (element) => element.getAttribute('data-id'),
223
+ renderHTML: (attributes) => attributes['id'] ? { 'data-id': attributes['id'] } : {},
224
+ },
225
+ label: {
226
+ default: '',
227
+ parseHTML: (element) => element.getAttribute('data-label') ?? element.textContent?.replace(/^@/u, '') ?? '',
228
+ renderHTML: (attributes) => attributes['label'] ? { 'data-label': attributes['label'] } : {},
229
+ },
230
+ avatar: {
231
+ default: null,
232
+ parseHTML: (element) => element.getAttribute('data-avatar'),
233
+ renderHTML: (attributes) => attributes['avatar'] ? { 'data-avatar': attributes['avatar'] } : {},
234
+ },
235
+ icon: {
236
+ default: null,
237
+ parseHTML: (element) => element.getAttribute('data-icon'),
238
+ renderHTML: (attributes) => attributes['icon'] ? { 'data-icon': attributes['icon'] } : {},
239
+ },
240
+ };
241
+ },
242
+ parseHTML() {
243
+ return [{ tag: 'span[data-mention]' }];
244
+ },
245
+ renderHTML({ node: mentionNode, HTMLAttributes }) {
246
+ return [
247
+ 'span',
248
+ core.mergeAttributes(HTMLAttributes, {
249
+ 'data-mention': '',
250
+ class: 'ax-text-editor-mention',
251
+ contenteditable: 'false',
252
+ dir: 'auto',
253
+ }),
254
+ `@${String(mentionNode.attrs['label'] ?? '')}`,
255
+ ];
256
+ },
257
+ renderText({ node: mentionNode }) {
258
+ return `@${String(mentionNode.attrs['label'] ?? '')}`;
259
+ },
260
+ addProseMirrorPlugins() {
261
+ return [plugin];
262
+ },
263
+ addNodeView() {
264
+ return ({ node: mentionNode }) => {
265
+ const dom = document.createElement('span');
266
+ let disposeAvatar;
267
+ const paint = (current) => {
268
+ disposeAvatar?.();
269
+ disposeAvatar = undefined;
270
+ dom.replaceChildren();
271
+ dom.className = 'ax-text-editor-mention';
272
+ dom.setAttribute('data-mention', '');
273
+ dom.setAttribute('data-id', String(current.attrs['id'] ?? ''));
274
+ dom.setAttribute('data-label', String(current.attrs['label'] ?? ''));
275
+ dom.contentEditable = 'false';
276
+ dom.dir = 'auto';
277
+ const item = {
278
+ id: String(current.attrs['id'] ?? ''),
279
+ label: String(current.attrs['label'] ?? ''),
280
+ avatar: textEditorMentionAvatar(typeof current.attrs['avatar'] === 'string' ? current.attrs['avatar'] : null) ?? undefined,
281
+ icon: typeof current.attrs['icon'] === 'string' ? current.attrs['icon'] : undefined,
282
+ };
283
+ const host = document.createElement('span');
284
+ host.className = 'ax-text-editor-mention-avatar-host';
285
+ dom.append(host);
286
+ const rendered = controller.renderMentionAvatar?.(host, item);
287
+ disposeAvatar = typeof rendered === 'function' ? rendered : undefined;
288
+ if (!disposeAvatar) {
289
+ const avatar = item.avatar ?? null;
290
+ if (avatar) {
291
+ const image = document.createElement('img');
292
+ image.src = avatar;
293
+ image.alt = '';
294
+ image.className = 'ax-text-editor-mention-avatar';
295
+ host.append(image);
296
+ }
297
+ else {
298
+ const fallback = document.createElement('span');
299
+ fallback.className = 'ax-text-editor-mention-avatar';
300
+ fallback.textContent = mentionInitial(item.label);
301
+ host.append(fallback);
302
+ }
303
+ }
304
+ const label = document.createElement('span');
305
+ label.textContent = `@${item.label}`;
306
+ dom.append(label);
307
+ };
308
+ paint(mentionNode);
309
+ return {
310
+ dom,
311
+ update(updated) {
312
+ if (updated.type.name !== 'mention')
313
+ return false;
314
+ paint(updated);
315
+ return true;
316
+ },
317
+ destroy() {
318
+ disposeAvatar?.();
319
+ },
320
+ };
321
+ };
322
+ },
323
+ });
324
+ const insert = (view, item) => {
325
+ const pluginState = key.getState(view.state);
326
+ const type = view.state.schema.nodes['mention'];
327
+ if (!pluginState?.active || !pluginState.range || !type)
328
+ return;
329
+ const { from, to } = pluginState.range;
330
+ const mentionNode = type.create({
331
+ id: item.id,
332
+ label: item.label,
333
+ avatar: textEditorMentionAvatar(item.avatar),
334
+ icon: item.icon ?? null,
335
+ });
336
+ let tr = view.state.tr.replaceWith(from, to, mentionNode);
337
+ const after = from + mentionNode.nodeSize;
338
+ const following = tr.doc.textBetween(after, Math.min(tr.doc.content.size, after + 1));
339
+ if (following !== ' ')
340
+ tr = tr.insertText(' ', after);
341
+ const caret = Math.min(after + 1, tr.doc.content.size);
342
+ tr = tr.setSelection(pmState.TextSelection.create(tr.doc, caret)).setMeta(key, { type: 'close' });
343
+ view.dispatch(tr);
344
+ view.focus();
345
+ };
346
+ plugin = new pmState.Plugin({
347
+ key,
348
+ state: {
349
+ init: (_config, state) => inactive(state),
350
+ apply(tr, prev, _old, state) {
351
+ const meta = tr.getMeta(key);
352
+ if (meta?.type === 'close')
353
+ return inactive(state);
354
+ if (meta?.type === 'items') {
355
+ if (!prev.active || meta.request !== prev.request)
356
+ return prev;
357
+ const items = meta.items;
358
+ return {
359
+ ...prev,
360
+ items,
361
+ index: items.length ? Math.min(prev.index, items.length - 1) : 0,
362
+ loading: false,
363
+ };
364
+ }
365
+ if (meta?.type === 'index') {
366
+ if (!prev.active || meta.index === prev.index)
367
+ return prev;
368
+ return { ...prev, index: meta.index };
369
+ }
370
+ if (composing)
371
+ return inactive(state, prev.dismissed);
372
+ const source = controller.getSource();
373
+ const trigger = mentionTrigger(source?.char);
374
+ const match = source && currentView?.editable !== false ? matchMention(state, trigger) : null;
375
+ let dismissed = prev.dismissed;
376
+ if (dismissed && tr.docChanged)
377
+ dismissed = { from: tr.mapping.map(dismissed.from), query: dismissed.query };
378
+ if (meta?.type === 'dismiss') {
379
+ return inactive(state, match ? { from: match.from, query: match.query } : dismissed);
380
+ }
381
+ if (!match || !source) {
382
+ if (!prev.active && !prev.dismissed)
383
+ return prev;
384
+ return inactive(state);
385
+ }
386
+ if (dismissed && dismissed.from === match.from && dismissed.query === match.query && meta?.type !== 'refresh') {
387
+ return inactive(state, dismissed);
388
+ }
389
+ if (prev.active && prev.query === match.query && (meta?.type !== 'refresh' || prev.loading)) {
390
+ const limit = source.limit ?? DEFAULT_MENTION_LIMIT;
391
+ if (prev.loading)
392
+ scheduleRequest(prev.request, prev.query, limit);
393
+ return {
394
+ ...prev,
395
+ range: { from: match.from, to: match.to },
396
+ dismissed: null,
397
+ decorations: decorate(pmView, state, match),
398
+ };
399
+ }
400
+ const request = ++requestSerial;
401
+ const query = match.query;
402
+ const limit = source.limit ?? DEFAULT_MENTION_LIMIT;
403
+ scheduleRequest(request, query, limit);
404
+ return {
405
+ active: true,
406
+ range: { from: match.from, to: match.to },
407
+ query,
408
+ items: [],
409
+ index: 0,
410
+ loading: true,
411
+ request,
412
+ dismissed: null,
413
+ decorations: decorate(pmView, state, match),
414
+ };
415
+ },
416
+ },
417
+ props: {
418
+ decorations(state) {
419
+ return key.getState(state)?.decorations ?? pmView.DecorationSet.empty;
420
+ },
421
+ handleKeyDown(view, event) {
422
+ const pluginState = key.getState(view.state);
423
+ if (!pluginState?.active || !view.editable)
424
+ return false;
425
+ if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
426
+ if (!pluginState.items.length)
427
+ return pluginState.loading;
428
+ const delta = event.key === 'ArrowDown' ? 1 : -1;
429
+ const index = (pluginState.index + delta + pluginState.items.length) % pluginState.items.length;
430
+ view.dispatch(view.state.tr.setMeta(key, { type: 'index', index }));
431
+ return true;
432
+ }
433
+ if (event.key === 'Enter' || event.key === 'Tab') {
434
+ const item = pluginState.items[pluginState.index];
435
+ if (!item)
436
+ return pluginState.loading;
437
+ event.preventDefault();
438
+ insert(view, item);
439
+ return true;
440
+ }
441
+ if (event.key === 'Escape') {
442
+ view.dispatch(view.state.tr.setMeta(key, { type: 'dismiss' }));
443
+ return true;
444
+ }
445
+ return false;
446
+ },
447
+ handleDOMEvents: {
448
+ compositionstart: () => {
449
+ composing = true;
450
+ return false;
451
+ },
452
+ compositionend: (view) => {
453
+ composing = false;
454
+ queueMicrotask(() => {
455
+ if (!alive || currentView !== view)
456
+ return;
457
+ view.dispatch(view.state.tr.setMeta(key, { type: 'refresh' }));
458
+ });
459
+ return false;
460
+ },
461
+ },
462
+ },
463
+ view(editorView) {
464
+ currentView = editorView;
465
+ alive = true;
466
+ controller.bind({
467
+ apply: (item) => {
468
+ if (currentView)
469
+ insert(currentView, item);
470
+ },
471
+ setIndex: (index) => {
472
+ const view = currentView;
473
+ const pluginState = view ? key.getState(view.state) : null;
474
+ if (!view || !pluginState?.active || index === pluginState.index)
475
+ return;
476
+ view.dispatch(view.state.tr.setMeta(key, { type: 'index', index }));
477
+ },
478
+ refresh: () => {
479
+ if (!currentView)
480
+ return;
481
+ currentView.dispatch(currentView.state.tr.setMeta(key, { type: 'refresh' }));
482
+ },
483
+ dismiss: () => {
484
+ const view = currentView;
485
+ if (!view || !key.getState(view.state)?.active)
486
+ return;
487
+ view.dispatch(view.state.tr.setMeta(key, { type: 'dismiss' }));
488
+ },
489
+ });
490
+ const reposition = () => {
491
+ if (alive && currentView)
492
+ publish(currentView);
493
+ };
494
+ document.addEventListener('scroll', reposition, true);
495
+ window.addEventListener('resize', reposition);
496
+ publish(editorView);
497
+ return {
498
+ update(view) {
499
+ currentView = view;
500
+ const pluginState = key.getState(view.state);
501
+ const source = controller.getSource();
502
+ if (pluginState?.active && pluginState.loading && source) {
503
+ scheduleRequest(pluginState.request, pluginState.query, source.limit ?? DEFAULT_MENTION_LIMIT);
504
+ }
505
+ publish(view);
506
+ },
507
+ destroy() {
508
+ alive = false;
509
+ currentView = null;
510
+ document.removeEventListener('scroll', reposition, true);
511
+ window.removeEventListener('resize', reposition);
512
+ controller.publish(null);
513
+ controller.bind({
514
+ apply: () => undefined,
515
+ setIndex: () => undefined,
516
+ refresh: () => undefined,
517
+ dismiss: () => undefined,
518
+ });
519
+ },
520
+ };
521
+ },
522
+ });
523
+ return node;
524
+ function publish(view) {
525
+ const pluginState = key.getState(view.state);
526
+ if (!pluginState?.active || view.editable === false) {
527
+ controller.publish(null);
528
+ return;
529
+ }
530
+ const rect = pluginState.range ? rectAt(view, pluginState.range.to) : null;
531
+ controller.publish({
532
+ query: pluginState.query,
533
+ items: pluginState.items,
534
+ index: pluginState.index,
535
+ loading: pluginState.loading,
536
+ rect,
537
+ });
538
+ }
539
+ }
540
+ function matchMention(state, char) {
541
+ const { selection } = state;
542
+ if (!selection.empty)
543
+ return null;
544
+ const $from = selection.$from;
545
+ if (!$from.parent.isTextblock || $from.parent.type.spec.code)
546
+ return null;
547
+ if ($from.marks().some((mark) => mark.type.spec.code || mark.type.name === 'code'))
548
+ return null;
549
+ const text = $from.parent.textBetween(0, $from.parentOffset, '\n', (leaf) => leaf.type.name === 'hardBreak' ? '\n' : '\ufffc');
550
+ const found = findTextEditorMentionQuery(text, char);
551
+ if (!found)
552
+ return null;
553
+ const from = $from.start() + found.start;
554
+ return { from, to: $from.pos, query: found.query };
555
+ }
556
+ function decorate(pmView, state, match) {
557
+ if (match.from >= match.to)
558
+ return pmView.DecorationSet.empty;
559
+ try {
560
+ return pmView.DecorationSet.create(state.doc, [
561
+ pmView.Decoration.inline(match.from, match.to, { class: 'ax-text-editor-mention-query' }),
562
+ ]);
563
+ }
564
+ catch {
565
+ return pmView.DecorationSet.empty;
566
+ }
567
+ }
568
+ function rectAt(view, pos) {
569
+ try {
570
+ const coords = view.coordsAtPos(pos);
571
+ return {
572
+ top: Math.round(coords.top),
573
+ left: Math.round(coords.left),
574
+ right: Math.round(coords.right),
575
+ bottom: Math.round(coords.bottom),
576
+ };
577
+ }
578
+ catch {
579
+ return null;
580
+ }
581
+ }
582
+ function mentionTrigger(char) {
583
+ const trigger = Array.from(char?.trim() || '@')[0];
584
+ return trigger && !/\s/u.test(trigger) ? trigger : '@';
585
+ }
586
+ function mentionInitial(label) {
587
+ const char = Array.from(label.trim())[0];
588
+ return char ? char.toLocaleUpperCase() : '@';
589
+ }
590
+ function escapeRegExp(value) {
591
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
592
+ }
593
+ function escapeCharClass(value) {
594
+ return value.replace(/[\\\]\-^]/g, '\\$&');
595
+ }
596
+
69
597
  class AXTextEditor {
70
598
  constructor() {
71
599
  this.editor = signal(null, /* @ts-ignore */
@@ -76,7 +604,13 @@ class AXTextEditor {
76
604
  ...(ngDevMode ? [{ debugName: "ready" }] : /* istanbul ignore next */ []));
77
605
  this.version = signal(0, /* @ts-ignore */
78
606
  ...(ngDevMode ? [{ debugName: "version" }] : /* istanbul ignore next */ []));
607
+ this.mentionSuggestion = signal(null, /* @ts-ignore */
608
+ ...(ngDevMode ? [{ debugName: "mentionSuggestion" }] : /* istanbul ignore next */ []));
79
609
  this.platformId = inject(PLATFORM_ID);
610
+ this.zone = inject(NgZone);
611
+ this.mentionSource = null;
612
+ this.mentionActions = null;
613
+ this.mentionAvatarRenderer = null;
80
614
  this.destroyed = false;
81
615
  this.applyingContent = false;
82
616
  this.commandsApi = null;
@@ -102,6 +636,7 @@ class AXTextEditor {
102
636
  features: options.features,
103
637
  headingLevels: options.headingLevels,
104
638
  extensions: options.extensions,
639
+ mentions: this,
105
640
  });
106
641
  if (this.destroyed) {
107
642
  return;
@@ -141,9 +676,12 @@ class AXTextEditor {
141
676
  },
142
677
  onBlur: ({ editor: ed, event }) => {
143
678
  this.refreshState(ed);
679
+ this.mentionActions?.dismiss();
144
680
  options.onBlur?.(ed, event);
145
681
  },
146
682
  });
683
+ if (options.exitMarksOnEnter)
684
+ editor.extensionManager.splittableMarks.splice(0);
147
685
  this.editor.set(editor);
148
686
  this.ready.set(true);
149
687
  this.refreshState(editor);
@@ -155,6 +693,8 @@ class AXTextEditor {
155
693
  this.editor.set(null);
156
694
  this.ready.set(false);
157
695
  this.state.set(AX_EMPTY_TEXT_EDITOR_STATE);
696
+ this.mentionSuggestion.set(null);
697
+ this.mentionActions = null;
158
698
  this.version.set(0);
159
699
  this.commandsApi = null;
160
700
  }
@@ -253,6 +793,12 @@ class AXTextEditor {
253
793
  this.refresh();
254
794
  return result;
255
795
  }
796
+ /** Inserts a `<br>` and keeps the inline marks that were active at the caret. */
797
+ insertHardBreak() {
798
+ const result = this.editor()?.chain().focus().setHardBreak().run() ?? false;
799
+ this.refresh();
800
+ return result;
801
+ }
256
802
  clearContent() {
257
803
  const result = this.editor()?.commands.clearContent() ?? false;
258
804
  this.refresh();
@@ -283,6 +829,53 @@ class AXTextEditor {
283
829
  setTextDirection(direction) {
284
830
  this.run('direction', { direction });
285
831
  }
832
+ /** Replaces the mention source. Pass `null` to close the picker and ignore `@`. */
833
+ setMentionSource(source) {
834
+ const previous = this.mentionSource;
835
+ this.mentionSource = source;
836
+ if (!source) {
837
+ this.mentionSuggestion.set(null);
838
+ return;
839
+ }
840
+ if (previous === source)
841
+ return;
842
+ queueMicrotask(() => this.mentionActions?.refresh());
843
+ }
844
+ selectMention(item) {
845
+ this.mentionActions?.apply(item);
846
+ }
847
+ setMentionIndex(index) {
848
+ this.mentionActions?.setIndex(index);
849
+ }
850
+ dismissMention() {
851
+ this.mentionActions?.dismiss();
852
+ }
853
+ /** Replaces the portrait drawn inside mention chips. Pass `null` for the built-in image. */
854
+ setMentionAvatarRenderer(renderer) {
855
+ this.mentionAvatarRenderer = renderer;
856
+ const editor = this.editor();
857
+ if (!editor || editor.isDestroyed)
858
+ return;
859
+ queueMicrotask(() => {
860
+ if (editor.isDestroyed)
861
+ return;
862
+ editor.view.dispatch(editor.state.tr.setMeta('axMentionAvatar', true));
863
+ });
864
+ }
865
+ renderMentionAvatar(host, item) {
866
+ return this.mentionAvatarRenderer?.(host, item);
867
+ }
868
+ getSource() {
869
+ return this.mentionSource;
870
+ }
871
+ publish(suggestion) {
872
+ if (mentionSuggestionsEqual(this.mentionSuggestion(), suggestion))
873
+ return;
874
+ this.zone.run(() => this.mentionSuggestion.set(suggestion));
875
+ }
876
+ bind(actions) {
877
+ this.mentionActions = actions;
878
+ }
286
879
  refreshState(editor) {
287
880
  this.version.update((value) => value + 1);
288
881
  const textColor = editor.getAttributes('textStyle')['color'];
@@ -309,6 +902,7 @@ class AXTextEditor {
309
902
  italic: editor.isActive('italic'),
310
903
  underline: editor.isActive('underline'),
311
904
  strike: editor.isActive('strike'),
905
+ spoiler: Boolean(editor.schema.marks['spoiler']) && editor.isActive('spoiler'),
312
906
  code: editor.isActive('code'),
313
907
  codeBlock: editor.isActive('codeBlock'),
314
908
  headingLevel,
@@ -395,6 +989,8 @@ function isTextEditorCommandActive(editor, command, attrs) {
395
989
  case 'redo':
396
990
  case 'horizontalRule':
397
991
  return false;
992
+ case 'spoiler':
993
+ return Boolean(editor.schema.marks['spoiler']) && editor.isActive('spoiler');
398
994
  default:
399
995
  return editor.isActive(command);
400
996
  }
@@ -476,6 +1072,8 @@ function canRunTextEditorCommand(editor, command, attrs) {
476
1072
  return editor.can().toggleUnderline();
477
1073
  case 'strike':
478
1074
  return editor.can().toggleStrike();
1075
+ case 'spoiler':
1076
+ return Boolean(editor.schema.marks['spoiler']) && editor.can().toggleSpoiler();
479
1077
  default:
480
1078
  return false;
481
1079
  }
@@ -583,6 +1181,8 @@ function runTextEditorCommand(editor, command, attrs) {
583
1181
  return chain.toggleUnderline().run();
584
1182
  case 'strike':
585
1183
  return chain.toggleStrike().run();
1184
+ case 'spoiler':
1185
+ return chain.toggleSpoiler().run();
586
1186
  default:
587
1187
  return false;
588
1188
  }
@@ -612,6 +1212,28 @@ function createSuperscript(core) {
612
1212
  },
613
1213
  });
614
1214
  }
1215
+ function createSpoiler(core) {
1216
+ return core.Mark.create({
1217
+ name: 'spoiler',
1218
+ inclusive: false,
1219
+ parseHTML() {
1220
+ return [{ tag: 'span[data-spoiler]' }];
1221
+ },
1222
+ renderHTML({ HTMLAttributes }) {
1223
+ return ['span', core.mergeAttributes(HTMLAttributes, { 'data-spoiler': '', class: 'ax-text-editor-spoiler' }), 0];
1224
+ },
1225
+ addCommands() {
1226
+ return {
1227
+ toggleSpoiler: () => ({ commands }) => commands.toggleMark(this.name),
1228
+ };
1229
+ },
1230
+ addKeyboardShortcuts() {
1231
+ return {
1232
+ 'Mod-Shift-p': () => this.editor.commands.toggleSpoiler(),
1233
+ };
1234
+ },
1235
+ });
1236
+ }
615
1237
  function createSubscript(core) {
616
1238
  return core.Mark.create({
617
1239
  name: 'subscript',
@@ -809,6 +1431,11 @@ function toVideoSrc(src) {
809
1431
  return trimmed;
810
1432
  }
811
1433
  const DEFAULT_HEADING_LEVELS = [1, 2, 3];
1434
+ const NO_MENTIONS = {
1435
+ getSource: () => null,
1436
+ publish: () => undefined,
1437
+ bind: () => undefined,
1438
+ };
812
1439
  function resolvePlaceholder(placeholder) {
813
1440
  if (typeof placeholder === 'function') {
814
1441
  return placeholder();
@@ -883,6 +1510,8 @@ async function createTextEditorExtensions(placeholderOrOptions = '') {
883
1510
  types: ['heading', 'paragraph'],
884
1511
  }),
885
1512
  createTextDirection(core),
1513
+ createSpoiler(core),
1514
+ createTextEditorMentionExtension({ core, pmState, pmView }, options.mentions ?? NO_MENTIONS),
886
1515
  ];
887
1516
  if (features.superscript) {
888
1517
  extensions.push(createSuperscript(core));
@@ -938,5 +1567,5 @@ var textEditor_extensions = /*#__PURE__*/Object.freeze({
938
1567
  * Generated bundle index. Do not edit.
939
1568
  */
940
1569
 
941
- export { AXTextEditor, AX_DEFAULT_TEXT_EDITOR_FEATURES, AX_EMPTY_TEXT_EDITOR_STATE, AX_TEXT_EDITOR_DEFAULT_BACKGROUND_COLORS, AX_TEXT_EDITOR_DEFAULT_TEXT_COLORS, AX_TEXT_EDITOR_FONT_SIZES, AX_TEXT_EDITOR_FONT_SIZE_TOKEN_VALUES, AX_TEXT_EDITOR_ZOOM_MAX, AX_TEXT_EDITOR_ZOOM_MIN, AX_TEXT_EDITOR_ZOOM_PRESETS, canRunTextEditorCommand, createTextEditorExtensions, isTextEditorCommandActive, runTextEditorCommand };
1570
+ export { AXTextEditor, AX_DEFAULT_TEXT_EDITOR_FEATURES, AX_EMPTY_TEXT_EDITOR_STATE, AX_TEXT_EDITOR_DEFAULT_BACKGROUND_COLORS, AX_TEXT_EDITOR_DEFAULT_TEXT_COLORS, AX_TEXT_EDITOR_FONT_SIZES, AX_TEXT_EDITOR_FONT_SIZE_TOKEN_VALUES, AX_TEXT_EDITOR_ZOOM_MAX, AX_TEXT_EDITOR_ZOOM_MIN, AX_TEXT_EDITOR_ZOOM_PRESETS, canRunTextEditorCommand, createTextEditorExtensions, filterTextEditorMentionItems, findTextEditorMentionQuery, isTextEditorCommandActive, mentionSuggestionsEqual, resolveTextEditorMentions, runTextEditorCommand, textEditorMentionAvatar };
942
1571
  //# sourceMappingURL=acorex-cdk-text-editor.mjs.map