@portabletext/editor 1.47.14 → 1.48.0
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-cjs/behavior.core.cjs +8 -9
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/editor-provider.cjs +171 -115
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js +8 -9
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/editor-provider.js +172 -116
- package/lib/_chunks-es/editor-provider.js.map +1 -1
- package/lib/behaviors/index.cjs +11 -11
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +11 -8
- package/lib/behaviors/index.d.ts +11 -8
- package/lib/behaviors/index.js +12 -12
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +4 -4
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +6 -4
- package/lib/index.d.ts +6 -4
- package/lib/index.js +5 -5
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs +7 -11
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +4 -3
- package/lib/plugins/index.d.ts +4 -3
- package/lib/plugins/index.js +8 -12
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.d.cts +4 -3
- package/lib/selectors/index.d.ts +4 -3
- package/lib/utils/index.d.cts +4 -3
- package/lib/utils/index.d.ts +4 -3
- package/package.json +3 -3
- package/src/behavior-actions/behavior.actions.ts +0 -9
- package/src/behaviors/behavior.core.dnd.ts +1 -1
- package/src/behaviors/behavior.default.ts +3 -3
- package/src/behaviors/behavior.emoji-picker.ts +7 -9
- package/src/behaviors/behavior.perform-event.ts +143 -76
- package/src/behaviors/behavior.types.action.ts +14 -11
- package/src/behaviors/index.ts +1 -1
- package/src/editor/components/Element.tsx +3 -5
- package/src/editor/editor-context.tsx +7 -0
- package/src/editor/editor-machine.ts +1 -0
- package/src/editor/editor-provider.tsx +1 -3
- package/src/editor/plugins/createWithUndoRedo.ts +8 -11
- package/src/editor/with-applying-behavior-actions.ts +9 -11
- package/src/internal-utils/global-scope.ts +19 -0
- package/src/internal-utils/globally-scoped-context.ts +39 -0
- package/src/plugins/plugin.decorator-shortcut.ts +3 -2
- package/src/plugins/plugin.one-line.tsx +3 -3
- package/src/behavior-actions/behavior.action.noop.ts +0 -5
|
@@ -19,29 +19,27 @@ export function isApplyingBehaviorActions(editor: Editor) {
|
|
|
19
19
|
|
|
20
20
|
////////
|
|
21
21
|
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
{actionSetId: string} | undefined
|
|
25
|
-
> = new WeakMap()
|
|
22
|
+
const CURRENT_UNDO_STEP: WeakMap<Editor, {undoStepId: string} | undefined> =
|
|
23
|
+
new WeakMap()
|
|
26
24
|
|
|
27
|
-
export function
|
|
28
|
-
const current =
|
|
25
|
+
export function withUndoStep(editor: Editor, fn: () => void) {
|
|
26
|
+
const current = CURRENT_UNDO_STEP.get(editor)
|
|
29
27
|
|
|
30
28
|
if (current) {
|
|
31
29
|
fn()
|
|
32
30
|
return
|
|
33
31
|
}
|
|
34
32
|
|
|
35
|
-
|
|
33
|
+
CURRENT_UNDO_STEP.set(
|
|
36
34
|
editor,
|
|
37
35
|
current ?? {
|
|
38
|
-
|
|
36
|
+
undoStepId: defaultKeyGenerator(),
|
|
39
37
|
},
|
|
40
38
|
)
|
|
41
39
|
fn()
|
|
42
|
-
|
|
40
|
+
CURRENT_UNDO_STEP.set(editor, undefined)
|
|
43
41
|
}
|
|
44
42
|
|
|
45
|
-
export function
|
|
46
|
-
return
|
|
43
|
+
export function getCurrentUndoStepId(editor: Editor) {
|
|
44
|
+
return CURRENT_UNDO_STEP.get(editor)?.undoStepId
|
|
47
45
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the global scope instance in a given environment.
|
|
3
|
+
*
|
|
4
|
+
* The strategy is to return the most modern, and if not, the most common:
|
|
5
|
+
* - The `globalThis` variable is the modern approach to accessing the global scope
|
|
6
|
+
* - The `window` variable is the global scope in a web browser
|
|
7
|
+
* - The `self` variable is the global scope in workers and others
|
|
8
|
+
* - The `global` variable is the global scope in Node.js
|
|
9
|
+
*/
|
|
10
|
+
function getGlobalScope() {
|
|
11
|
+
if (typeof globalThis !== 'undefined') return globalThis
|
|
12
|
+
if (typeof window !== 'undefined') return window
|
|
13
|
+
if (typeof self !== 'undefined') return self
|
|
14
|
+
if (typeof global !== 'undefined') return global
|
|
15
|
+
|
|
16
|
+
throw new Error('@portabletext/editor: could not locate global scope')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const globalScope = getGlobalScope() as any
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {createContext, type Context} from 'react'
|
|
2
|
+
import {globalScope} from './global-scope'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* As `@portabletext/editor` is declared as a dependency, and may be
|
|
6
|
+
* duplicated, sometimes across major versions it's critical that vital
|
|
7
|
+
* React Contexts are shared even when there is a duplicate.
|
|
8
|
+
*
|
|
9
|
+
* We have to support a Sanity Plugin being able to call hooks like
|
|
10
|
+
* `useEditor`, and then read the context setup by `sanity`, which calls
|
|
11
|
+
* `EditorProvider`, even if the provider and hook are different instances in
|
|
12
|
+
* memory.
|
|
13
|
+
*
|
|
14
|
+
* For this reason it's vital that all changes to globally scoped providers
|
|
15
|
+
* remain fully backwards compatible.
|
|
16
|
+
*/
|
|
17
|
+
export function createGloballyScopedContext<
|
|
18
|
+
ContextType,
|
|
19
|
+
const T extends ContextType = ContextType,
|
|
20
|
+
>(
|
|
21
|
+
/**
|
|
22
|
+
* Enforce that all Symbol.for keys used for globally scoped contexts have a predictable prefix
|
|
23
|
+
*/
|
|
24
|
+
key: `@portabletext/editor/context/${string}`,
|
|
25
|
+
defaultValue: T,
|
|
26
|
+
): Context<ContextType> {
|
|
27
|
+
const symbol = Symbol.for(key)
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Prevent errors about re-renders on React SSR on Next.js App Router
|
|
31
|
+
*/
|
|
32
|
+
if (typeof document === 'undefined') {
|
|
33
|
+
return createContext<ContextType>(defaultValue)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
globalScope[symbol] = globalScope[symbol] ?? createContext<T>(defaultValue)
|
|
37
|
+
|
|
38
|
+
return globalScope[symbol]
|
|
39
|
+
}
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
type CallbackLogicFunction,
|
|
9
9
|
} from 'xstate'
|
|
10
10
|
import {createDecoratorPairBehavior} from '../behaviors/behavior.decorator-pair'
|
|
11
|
-
import {effect, execute} from '../behaviors/behavior.types.action'
|
|
11
|
+
import {effect, execute, forward} from '../behaviors/behavior.types.action'
|
|
12
12
|
import {defineBehavior} from '../behaviors/behavior.types.behavior'
|
|
13
13
|
import type {Editor} from '../editor/create-editor'
|
|
14
14
|
import {useEditor} from '../editor/editor-provider'
|
|
@@ -108,13 +108,14 @@ const selectionListenerCallback: CallbackLogicFunction<
|
|
|
108
108
|
}
|
|
109
109
|
},
|
|
110
110
|
actions: [
|
|
111
|
-
(
|
|
111
|
+
({event}, {blockOffsets}) => [
|
|
112
112
|
{
|
|
113
113
|
type: 'effect',
|
|
114
114
|
effect: () => {
|
|
115
115
|
sendBack({type: 'selection', blockOffsets})
|
|
116
116
|
},
|
|
117
117
|
},
|
|
118
|
+
forward(event),
|
|
118
119
|
],
|
|
119
120
|
],
|
|
120
121
|
}),
|
|
@@ -21,7 +21,7 @@ const oneLineBehaviors = [
|
|
|
21
21
|
*/
|
|
22
22
|
defineBehavior({
|
|
23
23
|
on: 'insert.break',
|
|
24
|
-
actions: [
|
|
24
|
+
actions: [],
|
|
25
25
|
}),
|
|
26
26
|
/**
|
|
27
27
|
* `insert.block` `before` or `after` is not allowed in a one-line editor.
|
|
@@ -30,7 +30,7 @@ const oneLineBehaviors = [
|
|
|
30
30
|
on: 'insert.block',
|
|
31
31
|
guard: ({event}) =>
|
|
32
32
|
event.placement === 'before' || event.placement === 'after',
|
|
33
|
-
actions: [
|
|
33
|
+
actions: [],
|
|
34
34
|
}),
|
|
35
35
|
/**
|
|
36
36
|
* An ordinary `insert.block` is acceptable if it's a text block. In that
|
|
@@ -67,7 +67,7 @@ const oneLineBehaviors = [
|
|
|
67
67
|
*/
|
|
68
68
|
defineBehavior({
|
|
69
69
|
on: 'insert.block',
|
|
70
|
-
actions: [
|
|
70
|
+
actions: [],
|
|
71
71
|
}),
|
|
72
72
|
/**
|
|
73
73
|
* If multiple blocks are inserted, then the non-text blocks are filtered out
|