@portabletext/editor 2.13.1 → 2.13.2
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.
- package/lib/_chunks-dts/behavior.types.action.d.ts +10 -10
- package/lib/index.cjs +39 -5
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +39 -5
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.ts +3 -3
- package/package.json +5 -5
- package/src/editor/Editable.tsx +90 -1
package/lib/plugins/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Behavior, Editor, EditorEmittedEvent, EditorSchema } from "../_chunks-dts/behavior.types.action.js";
|
|
2
|
-
import * as
|
|
2
|
+
import * as react21 from "react";
|
|
3
3
|
import React from "react";
|
|
4
4
|
/**
|
|
5
5
|
* @beta
|
|
@@ -181,7 +181,7 @@ type MarkdownPluginConfig = MarkdownBehaviorsConfig & {
|
|
|
181
181
|
*/
|
|
182
182
|
declare function MarkdownPlugin(props: {
|
|
183
183
|
config: MarkdownPluginConfig;
|
|
184
|
-
}):
|
|
184
|
+
}): react21.JSX.Element;
|
|
185
185
|
/**
|
|
186
186
|
* @beta
|
|
187
187
|
* Restrict the editor to one line. The plugin takes care of blocking
|
|
@@ -192,5 +192,5 @@ declare function MarkdownPlugin(props: {
|
|
|
192
192
|
*
|
|
193
193
|
* @deprecated Install the plugin from `@portabletext/plugin-one-line`
|
|
194
194
|
*/
|
|
195
|
-
declare function OneLinePlugin():
|
|
195
|
+
declare function OneLinePlugin(): react21.JSX.Element;
|
|
196
196
|
export { BehaviorPlugin, DecoratorShortcutPlugin, EditorRefPlugin, EventListenerPlugin, MarkdownPlugin, type MarkdownPluginConfig, OneLinePlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/editor",
|
|
3
|
-
"version": "2.13.
|
|
3
|
+
"version": "2.13.2",
|
|
4
4
|
"description": "Portable Text Editor made in React",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -87,8 +87,8 @@
|
|
|
87
87
|
"xstate": "^5.22.0",
|
|
88
88
|
"@portabletext/block-tools": "^3.5.7",
|
|
89
89
|
"@portabletext/keyboard-shortcuts": "^1.1.1",
|
|
90
|
-
"@portabletext/
|
|
91
|
-
"@portabletext/
|
|
90
|
+
"@portabletext/schema": "^1.2.0",
|
|
91
|
+
"@portabletext/patches": "^1.1.8"
|
|
92
92
|
},
|
|
93
93
|
"devDependencies": {
|
|
94
94
|
"@sanity/diff-match-patch": "^3.2.0",
|
|
@@ -117,8 +117,8 @@
|
|
|
117
117
|
"vitest": "^3.2.4",
|
|
118
118
|
"vitest-browser-react": "^1.0.1",
|
|
119
119
|
"@portabletext/sanity-bridge": "1.1.11",
|
|
120
|
-
"
|
|
121
|
-
"
|
|
120
|
+
"@portabletext/test": "^0.0.0",
|
|
121
|
+
"racejar": "1.3.0"
|
|
122
122
|
},
|
|
123
123
|
"peerDependencies": {
|
|
124
124
|
"@portabletext/sanity-bridge": "^1.1.11",
|
package/src/editor/Editable.tsx
CHANGED
|
@@ -942,8 +942,28 @@ export const PortableTextEditable = forwardRef<
|
|
|
942
942
|
} else if (forwardedRef) {
|
|
943
943
|
forwardedRef.current = node
|
|
944
944
|
}
|
|
945
|
+
|
|
946
|
+
if (node) {
|
|
947
|
+
// Observe mutations (child list and subtree) to this component's DOM,
|
|
948
|
+
// and make sure the editor selection is valid when that happens.
|
|
949
|
+
const mutationObserver = new MutationObserver(() => {
|
|
950
|
+
validateSelection(slateEditor, node)
|
|
951
|
+
})
|
|
952
|
+
|
|
953
|
+
mutationObserver.observe(node, {
|
|
954
|
+
attributeOldValue: false,
|
|
955
|
+
attributes: false,
|
|
956
|
+
characterData: false,
|
|
957
|
+
childList: true,
|
|
958
|
+
subtree: true,
|
|
959
|
+
})
|
|
960
|
+
|
|
961
|
+
return () => {
|
|
962
|
+
mutationObserver.disconnect()
|
|
963
|
+
}
|
|
964
|
+
}
|
|
945
965
|
},
|
|
946
|
-
[forwardedRef],
|
|
966
|
+
[forwardedRef, slateEditor],
|
|
947
967
|
)
|
|
948
968
|
|
|
949
969
|
if (!portableTextEditor) {
|
|
@@ -987,3 +1007,72 @@ export const PortableTextEditable = forwardRef<
|
|
|
987
1007
|
})
|
|
988
1008
|
|
|
989
1009
|
PortableTextEditable.displayName = 'ForwardRef(PortableTextEditable)'
|
|
1010
|
+
|
|
1011
|
+
// This function will handle unexpected DOM changes inside the Editable rendering,
|
|
1012
|
+
// and make sure that we can maintain a stable slateEditor.selection when that happens.
|
|
1013
|
+
//
|
|
1014
|
+
// For example, if this Editable is rendered inside something that might re-render
|
|
1015
|
+
// this component (hidden contexts) while the user is still actively changing the
|
|
1016
|
+
// contentEditable, this could interfere with the intermediate DOM selection,
|
|
1017
|
+
// which again could be picked up by ReactEditor's event listeners.
|
|
1018
|
+
// If that range is invalid at that point, the slate.editorSelection could be
|
|
1019
|
+
// set either wrong, or invalid, to which slateEditor will throw exceptions
|
|
1020
|
+
// that are impossible to recover properly from or result in a wrong selection.
|
|
1021
|
+
//
|
|
1022
|
+
// Also the other way around, when the ReactEditor will try to create a DOM Range
|
|
1023
|
+
// from the current slateEditor.selection, it may throw unrecoverable errors
|
|
1024
|
+
// if the current editor.selection is invalid according to the DOM.
|
|
1025
|
+
// If this is the case, default to selecting the top of the document, if the
|
|
1026
|
+
// user already had a selection.
|
|
1027
|
+
function validateSelection(slateEditor: Editor, activeElement: HTMLDivElement) {
|
|
1028
|
+
if (!slateEditor.selection) {
|
|
1029
|
+
return
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
let root: Document | ShadowRoot | undefined
|
|
1033
|
+
|
|
1034
|
+
try {
|
|
1035
|
+
root = ReactEditor.findDocumentOrShadowRoot(slateEditor)
|
|
1036
|
+
} catch {}
|
|
1037
|
+
|
|
1038
|
+
if (!root) {
|
|
1039
|
+
// The editor has most likely been unmounted
|
|
1040
|
+
return
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
// Return if the editor isn't the active element
|
|
1044
|
+
if (activeElement !== root.activeElement) {
|
|
1045
|
+
return
|
|
1046
|
+
}
|
|
1047
|
+
const window = ReactEditor.getWindow(slateEditor)
|
|
1048
|
+
const domSelection = window.getSelection()
|
|
1049
|
+
if (!domSelection || domSelection.rangeCount === 0) {
|
|
1050
|
+
return
|
|
1051
|
+
}
|
|
1052
|
+
const existingDOMRange = domSelection.getRangeAt(0)
|
|
1053
|
+
try {
|
|
1054
|
+
const newDOMRange = ReactEditor.toDOMRange(
|
|
1055
|
+
slateEditor,
|
|
1056
|
+
slateEditor.selection,
|
|
1057
|
+
)
|
|
1058
|
+
if (
|
|
1059
|
+
newDOMRange.startOffset !== existingDOMRange.startOffset ||
|
|
1060
|
+
newDOMRange.endOffset !== existingDOMRange.endOffset
|
|
1061
|
+
) {
|
|
1062
|
+
debug('DOM range out of sync, validating selection')
|
|
1063
|
+
// Remove all ranges temporary
|
|
1064
|
+
domSelection?.removeAllRanges()
|
|
1065
|
+
// Set the correct range
|
|
1066
|
+
domSelection.addRange(newDOMRange)
|
|
1067
|
+
}
|
|
1068
|
+
} catch {
|
|
1069
|
+
debug(`Could not resolve selection, selecting top document`)
|
|
1070
|
+
// Deselect the editor
|
|
1071
|
+
Transforms.deselect(slateEditor)
|
|
1072
|
+
// Select top document if there is a top block to select
|
|
1073
|
+
if (slateEditor.children.length > 0) {
|
|
1074
|
+
Transforms.select(slateEditor, [0, 0])
|
|
1075
|
+
}
|
|
1076
|
+
slateEditor.onChange()
|
|
1077
|
+
}
|
|
1078
|
+
}
|