@portabletext/editor 3.3.0 → 3.3.1
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-es/use-editor.js +8 -4
- package/lib/_chunks-es/use-editor.js.map +1 -1
- package/lib/index.js +40 -6
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
- package/src/editor/PortableTextEditor.tsx +3 -1
- package/src/editor/plugins/createWithPatches.ts +4 -2
- package/src/editor/sync-machine.ts +8 -3
- package/src/internal-utils/apply-operation-to-portable-text.ts +3 -1
- package/src/internal-utils/event-position.ts +43 -4
- package/src/internal-utils/global-scope.ts +12 -4
- package/src/priority/priority.sort.ts +3 -1
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import React, { createContext } from "react";
|
|
2
2
|
function getGlobalScope() {
|
|
3
|
-
if (typeof globalThis < "u")
|
|
4
|
-
|
|
5
|
-
if (typeof
|
|
6
|
-
|
|
3
|
+
if (typeof globalThis < "u")
|
|
4
|
+
return globalThis;
|
|
5
|
+
if (typeof window < "u")
|
|
6
|
+
return window;
|
|
7
|
+
if (typeof self < "u")
|
|
8
|
+
return self;
|
|
9
|
+
if (typeof global < "u")
|
|
10
|
+
return global;
|
|
7
11
|
throw new Error("@portabletext/editor: could not locate global scope");
|
|
8
12
|
}
|
|
9
13
|
const globalScope = getGlobalScope();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-editor.js","sources":["../../src/internal-utils/global-scope.ts","../../src/internal-utils/globally-scoped-context.ts","../../src/editor/editor-context.tsx","../../src/editor/use-editor.ts"],"sourcesContent":["/**\n * Gets the global scope instance in a given environment.\n *\n * The strategy is to return the most modern, and if not, the most common:\n * - The `globalThis` variable is the modern approach to accessing the global scope\n * - The `window` variable is the global scope in a web browser\n * - The `self` variable is the global scope in workers and others\n * - The `global` variable is the global scope in Node.js\n */\nfunction getGlobalScope() {\n if (typeof globalThis !== 'undefined') return globalThis\n if (typeof window !== 'undefined') return window\n if (typeof self !== 'undefined') return self\n if (typeof global !== 'undefined') return global\n\n throw new Error('@portabletext/editor: could not locate global scope')\n}\n\nexport const globalScope = getGlobalScope() as any\n","import {createContext, type Context} from 'react'\nimport {globalScope} from './global-scope'\n\n/**\n * As `@portabletext/editor` is declared as a dependency, and may be\n * duplicated, sometimes across major versions it's critical that vital\n * React Contexts are shared even when there is a duplicate.\n *\n * We have to support a Sanity Plugin being able to call hooks like\n * `useEditor`, and then read the context setup by `sanity`, which calls\n * `EditorProvider`, even if the provider and hook are different instances in\n * memory.\n *\n * For this reason it's vital that all changes to globally scoped providers\n * remain fully backwards compatible.\n */\nexport function createGloballyScopedContext<\n ContextType,\n const T extends ContextType = ContextType,\n>(\n /**\n * Enforce that all Symbol.for keys used for globally scoped contexts have a predictable prefix\n */\n key: `@portabletext/editor/context/${string}`,\n defaultValue: T,\n): Context<ContextType> {\n const symbol = Symbol.for(key)\n\n /**\n * Prevent errors about re-renders on React SSR on Next.js App Router\n */\n if (typeof document === 'undefined') {\n return createContext<ContextType>(defaultValue)\n }\n\n globalScope[symbol] = globalScope[symbol] ?? createContext<T>(defaultValue)\n\n return globalScope[symbol]\n}\n","import type {Editor} from '../editor'\nimport {createGloballyScopedContext} from '../internal-utils/globally-scoped-context'\n\nexport const EditorContext = createGloballyScopedContext<Editor | null>(\n '@portabletext/editor/context/editor',\n null,\n)\n","import React from 'react'\nimport {EditorContext} from './editor-context'\n\n/**\n * @public\n * Get the current editor context from the `EditorProvider`.\n * Must be used inside the `EditorProvider` component.\n * @returns The current editor object.\n * @example\n * ```tsx\n * import { useEditor } from '@portabletext/editor'\n *\n * function MyComponent() {\n * const editor = useEditor()\n * }\n * ```\n * @group Hooks\n */\nexport function useEditor() {\n const editor = React.useContext(EditorContext)\n\n if (!editor) {\n throw new Error('No Editor set. Use EditorProvider to set one.')\n }\n\n return editor\n}\n"],"names":["getGlobalScope","globalThis","window","self","global","Error","globalScope","createGloballyScopedContext","key","defaultValue","symbol","Symbol","for","document","createContext","EditorContext","useEditor","editor","React","useContext"],"mappings":";AASA,SAASA,iBAAiB;AACxB,MAAI,OAAOC,aAAe,
|
|
1
|
+
{"version":3,"file":"use-editor.js","sources":["../../src/internal-utils/global-scope.ts","../../src/internal-utils/globally-scoped-context.ts","../../src/editor/editor-context.tsx","../../src/editor/use-editor.ts"],"sourcesContent":["/**\n * Gets the global scope instance in a given environment.\n *\n * The strategy is to return the most modern, and if not, the most common:\n * - The `globalThis` variable is the modern approach to accessing the global scope\n * - The `window` variable is the global scope in a web browser\n * - The `self` variable is the global scope in workers and others\n * - The `global` variable is the global scope in Node.js\n */\nfunction getGlobalScope() {\n if (typeof globalThis !== 'undefined') {\n return globalThis\n }\n if (typeof window !== 'undefined') {\n return window\n }\n if (typeof self !== 'undefined') {\n return self\n }\n if (typeof global !== 'undefined') {\n return global\n }\n\n throw new Error('@portabletext/editor: could not locate global scope')\n}\n\nexport const globalScope = getGlobalScope() as any\n","import {createContext, type Context} from 'react'\nimport {globalScope} from './global-scope'\n\n/**\n * As `@portabletext/editor` is declared as a dependency, and may be\n * duplicated, sometimes across major versions it's critical that vital\n * React Contexts are shared even when there is a duplicate.\n *\n * We have to support a Sanity Plugin being able to call hooks like\n * `useEditor`, and then read the context setup by `sanity`, which calls\n * `EditorProvider`, even if the provider and hook are different instances in\n * memory.\n *\n * For this reason it's vital that all changes to globally scoped providers\n * remain fully backwards compatible.\n */\nexport function createGloballyScopedContext<\n ContextType,\n const T extends ContextType = ContextType,\n>(\n /**\n * Enforce that all Symbol.for keys used for globally scoped contexts have a predictable prefix\n */\n key: `@portabletext/editor/context/${string}`,\n defaultValue: T,\n): Context<ContextType> {\n const symbol = Symbol.for(key)\n\n /**\n * Prevent errors about re-renders on React SSR on Next.js App Router\n */\n if (typeof document === 'undefined') {\n return createContext<ContextType>(defaultValue)\n }\n\n globalScope[symbol] = globalScope[symbol] ?? createContext<T>(defaultValue)\n\n return globalScope[symbol]\n}\n","import type {Editor} from '../editor'\nimport {createGloballyScopedContext} from '../internal-utils/globally-scoped-context'\n\nexport const EditorContext = createGloballyScopedContext<Editor | null>(\n '@portabletext/editor/context/editor',\n null,\n)\n","import React from 'react'\nimport {EditorContext} from './editor-context'\n\n/**\n * @public\n * Get the current editor context from the `EditorProvider`.\n * Must be used inside the `EditorProvider` component.\n * @returns The current editor object.\n * @example\n * ```tsx\n * import { useEditor } from '@portabletext/editor'\n *\n * function MyComponent() {\n * const editor = useEditor()\n * }\n * ```\n * @group Hooks\n */\nexport function useEditor() {\n const editor = React.useContext(EditorContext)\n\n if (!editor) {\n throw new Error('No Editor set. Use EditorProvider to set one.')\n }\n\n return editor\n}\n"],"names":["getGlobalScope","globalThis","window","self","global","Error","globalScope","createGloballyScopedContext","key","defaultValue","symbol","Symbol","for","document","createContext","EditorContext","useEditor","editor","React","useContext"],"mappings":";AASA,SAASA,iBAAiB;AACxB,MAAI,OAAOC,aAAe;AACxB,WAAOA;AAET,MAAI,OAAOC,SAAW;AACpB,WAAOA;AAET,MAAI,OAAOC,OAAS;AAClB,WAAOA;AAET,MAAI,OAAOC,SAAW;AACpB,WAAOA;AAGT,QAAM,IAAIC,MAAM,qDAAqD;AACvE;AAEO,MAAMC,cAAcN,eAAAA;ACVpB,SAASO,4BAOdC,KACAC,cACsB;AACtB,QAAMC,SAASC,OAAOC,IAAIJ,GAAG;AAK7B,SAAI,OAAOK,WAAa,MACfC,cAA2BL,YAAY,KAGhDH,YAAYI,MAAM,IAAIJ,YAAYI,MAAM,KAAKI,cAAiBL,YAAY,GAEnEH,YAAYI,MAAM;AAC3B;ACnCO,MAAMK,gBAAgBR,4BAC3B,uCACA,IACF;ACYO,SAAAS,YAAA;AACL,QAAAC,SAAeC,MAAKC,WAAYJ,aAAa;AAE7C,MAAI,CAACE;AACH,UAAM,IAAIZ,MAAM,+CAA+C;AAChE,SAEMY;AAAM;"}
|
package/lib/index.js
CHANGED
|
@@ -443,7 +443,15 @@ function getEventNode({
|
|
|
443
443
|
slateEditor,
|
|
444
444
|
event
|
|
445
445
|
}) {
|
|
446
|
-
|
|
446
|
+
if (!DOMEditor.hasTarget(slateEditor, event.target))
|
|
447
|
+
return;
|
|
448
|
+
let node;
|
|
449
|
+
try {
|
|
450
|
+
node = DOMEditor.toSlateNode(slateEditor, event.target);
|
|
451
|
+
} catch (error) {
|
|
452
|
+
console.error(error);
|
|
453
|
+
}
|
|
454
|
+
return node;
|
|
447
455
|
}
|
|
448
456
|
function getEventPositionBlock({
|
|
449
457
|
node,
|
|
@@ -455,7 +463,15 @@ function getEventPositionBlock({
|
|
|
455
463
|
});
|
|
456
464
|
if (!firstBlock)
|
|
457
465
|
return;
|
|
458
|
-
|
|
466
|
+
let firstBlockElement;
|
|
467
|
+
try {
|
|
468
|
+
firstBlockElement = DOMEditor.toDOMNode(slateEditor, firstBlock);
|
|
469
|
+
} catch (error) {
|
|
470
|
+
console.error(error);
|
|
471
|
+
}
|
|
472
|
+
if (!firstBlockElement)
|
|
473
|
+
return;
|
|
474
|
+
const firstBlockRect = firstBlockElement.getBoundingClientRect();
|
|
459
475
|
if (event.pageY < firstBlockRect.top)
|
|
460
476
|
return "start";
|
|
461
477
|
const [lastBlock] = getLastBlock({
|
|
@@ -463,10 +479,26 @@ function getEventPositionBlock({
|
|
|
463
479
|
});
|
|
464
480
|
if (!lastBlock)
|
|
465
481
|
return;
|
|
466
|
-
|
|
482
|
+
let lastBlockElement;
|
|
483
|
+
try {
|
|
484
|
+
lastBlockElement = DOMEditor.toDOMNode(slateEditor, lastBlock);
|
|
485
|
+
} catch (error) {
|
|
486
|
+
console.error(error);
|
|
487
|
+
}
|
|
488
|
+
if (!lastBlockElement)
|
|
489
|
+
return;
|
|
490
|
+
const lastBlockRef = lastBlockElement.getBoundingClientRect();
|
|
467
491
|
if (event.pageY > lastBlockRef.bottom)
|
|
468
492
|
return "end";
|
|
469
|
-
|
|
493
|
+
let element;
|
|
494
|
+
try {
|
|
495
|
+
element = DOMEditor.toDOMNode(slateEditor, node);
|
|
496
|
+
} catch (error) {
|
|
497
|
+
console.error(error);
|
|
498
|
+
}
|
|
499
|
+
if (!element)
|
|
500
|
+
return;
|
|
501
|
+
const elementRect = element.getBoundingClientRect(), top = elementRect.top, height = elementRect.height;
|
|
470
502
|
return Math.abs(top - event.pageY) < height / 2 ? "start" : "end";
|
|
471
503
|
}
|
|
472
504
|
function getEventSelection({
|
|
@@ -6633,7 +6665,8 @@ function applyOperationToPortableTextImmutable(context, root, operation) {
|
|
|
6633
6665
|
offset,
|
|
6634
6666
|
text
|
|
6635
6667
|
} = operation;
|
|
6636
|
-
if (text.length === 0)
|
|
6668
|
+
if (text.length === 0)
|
|
6669
|
+
return root;
|
|
6637
6670
|
const span = getSpan(context, root, path);
|
|
6638
6671
|
if (!span)
|
|
6639
6672
|
return root;
|
|
@@ -12822,7 +12855,8 @@ class PortableTextEditor extends Component {
|
|
|
12822
12855
|
static insertBreak = (editor) => editor.editable?.insertBreak();
|
|
12823
12856
|
static isVoid = (editor, element) => editor.editable?.isVoid(element);
|
|
12824
12857
|
static isObjectPath = (_editor, path) => {
|
|
12825
|
-
if (!path || !Array.isArray(path))
|
|
12858
|
+
if (!path || !Array.isArray(path))
|
|
12859
|
+
return !1;
|
|
12826
12860
|
const isChildObjectEditPath = path.length > 3 && path[1] === "children";
|
|
12827
12861
|
return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
|
|
12828
12862
|
};
|