@kids-reporter/draft-editor 1.0.33 → 1.0.35

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.
@@ -8,6 +8,7 @@ var _draftRenderer = require("@kids-reporter/draft-renderer");
8
8
  var _draftJs = require("draft-js");
9
9
  var _react = require("react");
10
10
  var _annotation = require("../entity-decorators/annotation");
11
+ var _linkTrailingBuffer = require("../utils/link-trailing-buffer");
11
12
  var _jsxRuntime = require("react/jsx-runtime");
12
13
  const AnnotationButton = props => {
13
14
  const toggleEntity = _draftJs.RichUtils.toggleLink;
@@ -35,7 +36,9 @@ const AnnotationButton = props => {
35
36
  const newEditorState = _draftJs.EditorState.set(editorStateOfOuterEditor, {
36
37
  currentContent: contentStateWithEntity
37
38
  });
38
- onChange(toggleEntity(newEditorState, newEditorState.getSelection(), entityKey));
39
+ let next = toggleEntity(newEditorState, newEditorState.getSelection(), entityKey);
40
+ next = (0, _linkTrailingBuffer.appendLinkCreateTrailingBuffer)(next);
41
+ onChange(next);
39
42
  setToShowInput(false);
40
43
  props.onEditFinish();
41
44
  };
@@ -8,6 +8,7 @@ var _draftRenderer = require("@kids-reporter/draft-renderer");
8
8
  var _draftJs = require("draft-js");
9
9
  var _react = require("react");
10
10
  var _link = require("../entity-decorators/link");
11
+ var _linkTrailingBuffer = require("../utils/link-trailing-buffer");
11
12
  var _jsxRuntime = require("react/jsx-runtime");
12
13
  const LinkButton = props => {
13
14
  const {
@@ -33,7 +34,9 @@ const LinkButton = props => {
33
34
  const newEditorState = _draftJs.EditorState.set(editorState, {
34
35
  currentContent: contentStateWithEntity
35
36
  });
36
- onChange(_draftJs.RichUtils.toggleLink(newEditorState, newEditorState.getSelection(), entityKey));
37
+ let next = _draftJs.RichUtils.toggleLink(newEditorState, newEditorState.getSelection(), entityKey);
38
+ next = (0, _linkTrailingBuffer.appendLinkCreateTrailingBuffer)(next);
39
+ onChange(next);
37
40
  setToShowUrlInput(false);
38
41
  props.onEditFinish();
39
42
  };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.appendLinkCreateTrailingBuffer = appendLinkCreateTrailingBuffer;
7
+ var _draftJs = require("draft-js");
8
+ /** Draft has no zero-length entity; ZWSP gives a neutral tail after a new mutable inline span. */
9
+ const LINK_TRAILING_BUFFER = '\u200b';
10
+
11
+ /**
12
+ * After toggleLink (link, annotation, etc.), collapse to range end and insert
13
+ * a zero-width buffer without inline entity so IME / typing can continue outside the span.
14
+ * Skips insertion when a ZWSP already sits at the insertion point, or the caret
15
+ * is after a trailing ZWSP at block end (avoids accumulation on repeat apply).
16
+ */
17
+ function appendLinkCreateTrailingBuffer(editorState) {
18
+ const sel = editorState.getSelection();
19
+ const endKey = sel.getEndKey();
20
+ const endOffset = sel.getEndOffset();
21
+ const block = editorState.getCurrentContent().getBlockForKey(endKey);
22
+ const text = block.getText();
23
+ const nextIsZwsp = endOffset < text.length && text.charAt(endOffset) === LINK_TRAILING_BUFFER;
24
+ const atBlockEndAfterZwsp = endOffset === text.length && endOffset > 0 && text.charAt(endOffset - 1) === LINK_TRAILING_BUFFER;
25
+ if (nextIsZwsp || atBlockEndAfterZwsp) {
26
+ return editorState;
27
+ }
28
+ const collapsed = sel.merge({
29
+ anchorKey: endKey,
30
+ anchorOffset: endOffset,
31
+ focusKey: endKey,
32
+ focusOffset: endOffset,
33
+ isBackward: false
34
+ });
35
+ const withSelection = _draftJs.EditorState.acceptSelection(editorState, collapsed);
36
+ const content = _draftJs.Modifier.insertText(withSelection.getCurrentContent(), withSelection.getSelection(), LINK_TRAILING_BUFFER, withSelection.getCurrentInlineStyle(), undefined);
37
+ return _draftJs.EditorState.push(withSelection, content, 'insert-characters');
38
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kids-reporter/draft-editor",
3
- "version": "1.0.33",
3
+ "version": "1.0.35",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {