slate-angular 19.1.0-next.3 → 19.1.0-next.4

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,5 +1,5 @@
1
1
  import { Editor, Range, Element, Transforms, Text as Text$1, Node, Path } from 'slate';
2
- import { EDITOR_TO_ELEMENT, NODE_TO_ELEMENT, DOMEditor, normalizeDOMPoint, isDOMSelection, IS_CHROME as IS_CHROME$1, hasShadowRoot, isDOMElement, NODE_TO_PARENT, NODE_TO_INDEX, isDOMNode, withDOM, NODE_TO_KEY, ELEMENT_TO_NODE, getDefaultView, EDITOR_TO_WINDOW, IS_READ_ONLY, EDITOR_TO_ON_CHANGE, IS_FOCUSED, TRIPLE_CLICK, isPlainTextOnlyPaste } from 'slate-dom';
2
+ import { EDITOR_TO_ELEMENT, NODE_TO_ELEMENT, DOMEditor, normalizeDOMPoint, isDOMSelection, IS_CHROME as IS_CHROME$1, hasShadowRoot, isDOMElement, NODE_TO_PARENT, NODE_TO_INDEX, isDOMNode, IS_FOCUSED, withDOM, NODE_TO_KEY, ELEMENT_TO_NODE, getDefaultView, EDITOR_TO_WINDOW, IS_READ_ONLY, EDITOR_TO_ON_CHANGE, TRIPLE_CLICK, isPlainTextOnlyPaste } from 'slate-dom';
3
3
  import { isKeyHotkey } from 'is-hotkey';
4
4
  import * as i0 from '@angular/core';
5
5
  import { TemplateRef, ViewChild, ChangeDetectionStrategy, Component, InjectionToken, ComponentRef, IterableDiffers, inject, ElementRef, ChangeDetectorRef, Input, Directive, HostBinding, ViewContainerRef, forwardRef, Inject, NgModule } from '@angular/core';
@@ -353,6 +353,53 @@ const CustomDOMEditor = {
353
353
  }
354
354
  };
355
355
 
356
+ const SlateFragmentAttributeKey = 'data-slate-angular-fragment';
357
+ /**
358
+ * Get x-slate-fragment attribute from data-slate-angular-fragment
359
+ */
360
+ const catchSlateFragment = /data-slate-angular-fragment="(.+?)"/m;
361
+ const getSlateFragmentAttribute = (htmlData) => {
362
+ const [, fragment] = htmlData.match(catchSlateFragment) || [];
363
+ return fragment;
364
+ };
365
+ /**
366
+ * Check if a DOM node is an element node.
367
+ */
368
+ const isDOMText = (value) => {
369
+ return isDOMNode(value) && value.nodeType === 3;
370
+ };
371
+ /**
372
+ * Get a plaintext representation of the content of a node, accounting for block
373
+ * elements which get a newline appended.
374
+ *
375
+ * The domNode must be attached to the DOM.
376
+ */
377
+ const getPlainText = (domNode) => {
378
+ let text = '';
379
+ if (isDOMText(domNode) && domNode.nodeValue) {
380
+ return domNode.nodeValue;
381
+ }
382
+ if (isDOMElement(domNode)) {
383
+ for (const childNode of Array.from(domNode.childNodes)) {
384
+ text += getPlainText(childNode);
385
+ }
386
+ const display = getComputedStyle(domNode).getPropertyValue('display');
387
+ if (display === 'block' || display === 'list' || domNode.tagName === 'BR') {
388
+ text += '\n';
389
+ }
390
+ }
391
+ return text;
392
+ };
393
+ /**
394
+ * Get the dom selection from Shadow Root if possible, otherwise from the document
395
+ */
396
+ const getSelection = (root) => {
397
+ if (root.getSelection != null) {
398
+ return root.getSelection();
399
+ }
400
+ return document.getSelection();
401
+ };
402
+
356
403
  const AngularEditor = {
357
404
  ...CustomDOMEditor,
358
405
  /**
@@ -405,6 +452,36 @@ const AngularEditor = {
405
452
  offset: options.direction === 'left' ? FAKE_LEFT_BLOCK_CARD_OFFSET : FAKE_RIGHT_BLOCK_CARD_OFFSET
406
453
  };
407
454
  Transforms.select(editor, { anchor: cursor, focus: cursor });
455
+ },
456
+ focus: (editor, options = { retries: 5 }) => {
457
+ // Return if already focused
458
+ if (IS_FOCUSED.get(editor)) {
459
+ return;
460
+ }
461
+ // Return if no dom node is associated with the editor, which means the editor is not yet mounted
462
+ // or has been unmounted. This can happen especially, while retrying to focus the editor.
463
+ if (!EDITOR_TO_ELEMENT.get(editor)) {
464
+ return;
465
+ }
466
+ const el = DOMEditor.toDOMNode(editor, editor);
467
+ const root = DOMEditor.findDocumentOrShadowRoot(editor);
468
+ if (root.activeElement !== el) {
469
+ // Ensure that the DOM selection state is set to the editor's selection
470
+ if (editor.selection && root instanceof Document) {
471
+ const domSelection = getSelection(root);
472
+ const domRange = DOMEditor.toDOMRange(editor, editor.selection);
473
+ domSelection?.removeAllRanges();
474
+ domSelection?.addRange(domRange);
475
+ }
476
+ // Create a new selection in the top of the document if missing
477
+ if (!editor.selection) {
478
+ Transforms.select(editor, Editor.start(editor, []));
479
+ }
480
+ // IS_FOCUSED should be set before calling el.focus() to ensure that
481
+ // FocusedContext is updated to the correct value
482
+ IS_FOCUSED.set(editor, true);
483
+ el.focus({ preventScroll: true });
484
+ }
408
485
  }
409
486
  };
410
487
 
@@ -750,44 +827,6 @@ const getNavigatorClipboard = async () => {
750
827
  return clipboardData;
751
828
  };
752
829
 
753
- const SlateFragmentAttributeKey = 'data-slate-angular-fragment';
754
- /**
755
- * Get x-slate-fragment attribute from data-slate-angular-fragment
756
- */
757
- const catchSlateFragment = /data-slate-angular-fragment="(.+?)"/m;
758
- const getSlateFragmentAttribute = (htmlData) => {
759
- const [, fragment] = htmlData.match(catchSlateFragment) || [];
760
- return fragment;
761
- };
762
- /**
763
- * Check if a DOM node is an element node.
764
- */
765
- const isDOMText = (value) => {
766
- return isDOMNode(value) && value.nodeType === 3;
767
- };
768
- /**
769
- * Get a plaintext representation of the content of a node, accounting for block
770
- * elements which get a newline appended.
771
- *
772
- * The domNode must be attached to the DOM.
773
- */
774
- const getPlainText = (domNode) => {
775
- let text = '';
776
- if (isDOMText(domNode) && domNode.nodeValue) {
777
- return domNode.nodeValue;
778
- }
779
- if (isDOMElement(domNode)) {
780
- for (const childNode of Array.from(domNode.childNodes)) {
781
- text += getPlainText(childNode);
782
- }
783
- const display = getComputedStyle(domNode).getPropertyValue('display');
784
- if (display === 'block' || display === 'list' || domNode.tagName === 'BR') {
785
- text += '\n';
786
- }
787
- }
788
- return text;
789
- };
790
-
791
830
  const buildHTMLText = (wrapper, attach, data) => {
792
831
  const stringObj = JSON.stringify(data);
793
832
  const encoded = window.btoa(encodeURIComponent(stringObj));
@@ -4000,5 +4039,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.14", ngImpo
4000
4039
  * Generated bundle index. Do not edit.
4001
4040
  */
4002
4041
 
4003
- export { AngularEditor, BaseComponent, BaseElementComponent, BaseLeafComponent, BaseTextComponent, EDITOR_TO_AFTER_VIEW_INIT_QUEUE, ELEMENT_TO_COMPONENT, FAKE_LEFT_BLOCK_CARD_OFFSET, FAKE_RIGHT_BLOCK_CARD_OFFSET, HAS_BEFORE_INPUT_SUPPORT, IS_ANDROID, IS_APPLE, IS_CHROME, IS_CHROME_LEGACY, IS_EDGE_LEGACY, IS_FIREFOX, IS_FIREFOX_LEGACY, IS_IOS, IS_QQBROWSER, IS_SAFARI, IS_UC_MOBILE, IS_WECHATBROWSER, PLACEHOLDER_SYMBOL, SlateChildren, SlateChildrenOutlet, SlateDefaultString, SlateEditable, SlateElement, SlateErrorCode, SlateFragmentAttributeKey, SlateLeaves, SlateModule, SlateString, blobAsString, buildHTMLText, check, completeTable, createClipboardData, createThrottleRAF, defaultScrollSelectionIntoView, getCardTargetAttribute, getClipboardData, getClipboardFromHTMLText, getDataTransferClipboard, getDataTransferClipboardText, getNavigatorClipboard, getPlainText, getSlateFragmentAttribute, hasAfterContextChange, hasBeforeContextChange, hasBlockCard, hasBlockCardWithNode, hotkeys, isCardCenterByTargetAttr, isCardLeft, isCardLeftByTargetAttr, isCardRightByTargetAttr, isClipboardFile, isClipboardReadSupported, isClipboardWriteSupported, isClipboardWriteTextSupported, isComponentType, isDOMText, isDecoratorRangeListEqual, isEmpty, isInvalidTable, isTemplateRef, isValid, normalize, setClipboardData, setDataTransferClipboard, setDataTransferClipboardText, setNavigatorClipboard, shallowCompare, stripHtml, withAngular };
4042
+ export { AngularEditor, BaseComponent, BaseElementComponent, BaseLeafComponent, BaseTextComponent, EDITOR_TO_AFTER_VIEW_INIT_QUEUE, ELEMENT_TO_COMPONENT, FAKE_LEFT_BLOCK_CARD_OFFSET, FAKE_RIGHT_BLOCK_CARD_OFFSET, HAS_BEFORE_INPUT_SUPPORT, IS_ANDROID, IS_APPLE, IS_CHROME, IS_CHROME_LEGACY, IS_EDGE_LEGACY, IS_FIREFOX, IS_FIREFOX_LEGACY, IS_IOS, IS_QQBROWSER, IS_SAFARI, IS_UC_MOBILE, IS_WECHATBROWSER, PLACEHOLDER_SYMBOL, SlateChildren, SlateChildrenOutlet, SlateDefaultString, SlateEditable, SlateElement, SlateErrorCode, SlateFragmentAttributeKey, SlateLeaves, SlateModule, SlateString, blobAsString, buildHTMLText, check, completeTable, createClipboardData, createThrottleRAF, defaultScrollSelectionIntoView, getCardTargetAttribute, getClipboardData, getClipboardFromHTMLText, getDataTransferClipboard, getDataTransferClipboardText, getNavigatorClipboard, getPlainText, getSelection, getSlateFragmentAttribute, hasAfterContextChange, hasBeforeContextChange, hasBlockCard, hasBlockCardWithNode, hotkeys, isCardCenterByTargetAttr, isCardLeft, isCardLeftByTargetAttr, isCardRightByTargetAttr, isClipboardFile, isClipboardReadSupported, isClipboardWriteSupported, isClipboardWriteTextSupported, isComponentType, isDOMText, isDecoratorRangeListEqual, isEmpty, isInvalidTable, isTemplateRef, isValid, normalize, setClipboardData, setDataTransferClipboard, setDataTransferClipboardText, setNavigatorClipboard, shallowCompare, stripHtml, withAngular };
4004
4043
  //# sourceMappingURL=slate-angular.mjs.map