@portabletext/editor 1.47.14 → 1.47.15

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/index.d.cts CHANGED
@@ -63,6 +63,7 @@ import {
63
63
  } from 'xstate'
64
64
  import type {EventObject, Snapshot} from 'xstate'
65
65
  import {GuardArgs} from 'xstate/guards'
66
+ import {Editor as Editor_2} from './create-editor'
66
67
 
67
68
  declare type AbstractBehaviorEvent =
68
69
  | {
@@ -6815,7 +6816,7 @@ declare type UnsetEvent = {
6815
6816
  * ```
6816
6817
  * @group Hooks
6817
6818
  */
6818
- export declare function useEditor(): Editor
6819
+ export declare function useEditor(): Editor_2
6819
6820
 
6820
6821
  /**
6821
6822
  * @public
package/lib/index.d.ts CHANGED
@@ -63,6 +63,7 @@ import {
63
63
  } from 'xstate'
64
64
  import type {EventObject, Snapshot} from 'xstate'
65
65
  import {GuardArgs} from 'xstate/guards'
66
+ import {Editor as Editor_2} from './create-editor'
66
67
 
67
68
  declare type AbstractBehaviorEvent =
68
69
  | {
@@ -6815,7 +6816,7 @@ declare type UnsetEvent = {
6815
6816
  * ```
6816
6817
  * @group Hooks
6817
6818
  */
6818
- export declare function useEditor(): Editor
6819
+ export declare function useEditor(): Editor_2
6819
6820
 
6820
6821
  /**
6821
6822
  * @public
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.47.14",
3
+ "version": "1.47.15",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -79,8 +79,8 @@
79
79
  "slate-react": "0.112.1",
80
80
  "use-effect-event": "^1.0.2",
81
81
  "xstate": "^5.19.2",
82
- "@portabletext/patches": "1.1.3",
83
- "@portabletext/block-tools": "1.1.21"
82
+ "@portabletext/block-tools": "1.1.21",
83
+ "@portabletext/patches": "1.1.3"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@portabletext/toolkit": "^2.0.17",
@@ -0,0 +1,7 @@
1
+ import {createGloballyScopedContext} from '../internal-utils/globally-scoped-context'
2
+ import type {Editor} from './create-editor'
3
+
4
+ export const EditorContext = createGloballyScopedContext<Editor | null>(
5
+ '@portabletext/editor/context/editor',
6
+ null,
7
+ )
@@ -5,10 +5,10 @@ import {Synchronizer} from './components/Synchronizer'
5
5
  import {
6
6
  createInternalEditor,
7
7
  editorConfigToMachineInput,
8
- type Editor,
9
8
  type EditorConfig,
10
9
  } from './create-editor'
11
10
  import {EditorActorContext} from './editor-actor-context'
11
+ import {EditorContext} from './editor-context'
12
12
  import {editorMachine} from './editor-machine'
13
13
  import {PortableTextEditorContext} from './hooks/usePortableTextEditor'
14
14
  import {PortableTextEditorSelectionProvider} from './hooks/usePortableTextEditorSelection'
@@ -18,8 +18,6 @@ import {
18
18
  type PortableTextEditorProps,
19
19
  } from './PortableTextEditor'
20
20
 
21
- const EditorContext = React.createContext<Editor | undefined>(undefined)
22
-
23
21
  /**
24
22
  * @public
25
23
  */
@@ -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
+ }