@kids-reporter/draft-editor 1.0.34 → 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
|
-
|
|
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
|
};
|
|
@@ -5,20 +5,31 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.appendLinkCreateTrailingBuffer = appendLinkCreateTrailingBuffer;
|
|
7
7
|
var _draftJs = require("draft-js");
|
|
8
|
-
/** Draft has no zero-length entity; ZWSP gives a neutral tail after a new
|
|
8
|
+
/** Draft has no zero-length entity; ZWSP gives a neutral tail after a new mutable inline span. */
|
|
9
9
|
const LINK_TRAILING_BUFFER = '\u200b';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* After toggleLink, collapse to range end and insert
|
|
13
|
-
* without
|
|
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).
|
|
14
16
|
*/
|
|
15
17
|
function appendLinkCreateTrailingBuffer(editorState) {
|
|
16
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
|
+
}
|
|
17
28
|
const collapsed = sel.merge({
|
|
18
|
-
anchorKey:
|
|
19
|
-
anchorOffset:
|
|
20
|
-
focusKey:
|
|
21
|
-
focusOffset:
|
|
29
|
+
anchorKey: endKey,
|
|
30
|
+
anchorOffset: endOffset,
|
|
31
|
+
focusKey: endKey,
|
|
32
|
+
focusOffset: endOffset,
|
|
22
33
|
isBackward: false
|
|
23
34
|
});
|
|
24
35
|
const withSelection = _draftJs.EditorState.acceptSelection(editorState, collapsed);
|