Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions packages/editor/src/editor/create-editable-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import {
slateRangeToSelection,
} from '../internal-utils/slate-utils'
import {toSlateRange} from '../internal-utils/to-slate-range'
import {getTextBlockNode} from '../node-traversal/get-text-block-node'
import {getActiveAnnotationsMarks} from '../selectors/selector.get-active-annotation-marks'
import {getActiveDecorators} from '../selectors/selector.get-active-decorators'
import {getFocusBlock} from '../selectors/selector.get-focus-block'
import {getFocusSpan} from '../selectors/selector.get-focus-span'
import {getSelectedValue} from '../selectors/selector.get-selected-value'
import {isActiveAnnotation} from '../selectors/selector.is-active-annotation'
import {node as editorNode} from '../slate/editor/node'
import {nodes} from '../slate/editor/nodes'
import {path as editorPath} from '../slate/editor/path'
import {isCollapsedRange} from '../slate/range/is-collapsed-range'
import {isExpandedRange} from '../slate/range/is-expanded-range'
import {isRange} from '../slate/range/is-range'
Expand Down Expand Up @@ -318,19 +319,25 @@ export function createEditableAPI(
node.marks.length > 0,
})
for (const [span, path] of spans) {
const [block] = editorNode(editor, path, {depth: 1})
if (isTextBlock({schema: editor.schema}, block)) {
block.markDefs?.forEach((def) => {
if (
isSpan({schema: editor.schema}, span) &&
span.marks &&
Array.isArray(span.marks) &&
span.marks.includes(def._key)
) {
activeAnnotations.push(def)
}
})
const blockEntry = getTextBlockNode(
{schema: editor.schema, editableTypes: editor.editableTypes},
editor,
editorPath(editor, path, {depth: 1}),
)
if (!blockEntry) {
continue
}
const [block] = blockEntry
block.markDefs?.forEach((def) => {
if (
isSpan({schema: editor.schema}, span) &&
span.marks &&
Array.isArray(span.marks) &&
span.marks.includes(def._key)
) {
activeAnnotations.push(def)
}
})
}
return activeAnnotations
} catch {
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/editor/create-slate-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export function createSlateEditor(
keyGenerator: context.keyGenerator,
})

editor.editableTypes = new Set()

editor.decoratedRanges = []
editor.decoratorState = {}
editor.blockIndexMap = new Map<string, number>()
Expand Down
73 changes: 61 additions & 12 deletions packages/editor/src/editor/sync-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import {
import {safeStringify} from '../internal-utils/safe-json'
import {validateValue} from '../internal-utils/validateValue'
import {toSlateBlock} from '../internal-utils/values'
import {getNode} from '../node-traversal/get-node'
import {hasNode} from '../node-traversal/has-node'
import {withRemoteChanges} from '../slate-plugins/slate-plugin.remote-changes'
import {pluginWithoutHistory} from '../slate-plugins/slate-plugin.without-history'
import {withoutPatching} from '../slate-plugins/slate-plugin.without-patching'
import {deleteText} from '../slate/core/delete-text'
import {node as editorNode} from '../slate/editor/node'
import {start} from '../slate/editor/start'
import {withoutNormalizing} from '../slate/editor/without-normalizing'
import type {Node} from '../slate/interfaces/node'
import {hasNode} from '../slate/node/has-node'
import type {PickFromUnion} from '../type-utils'
import type {InvalidValueResolution} from '../types/editor'
import type {PortableTextSlateEditor} from '../types/slate-editor'
Expand Down Expand Up @@ -612,7 +612,18 @@ function clearEditor({

slateEditor.children.forEach((_, index) => {
const removePath = [childrenLength - 1 - index]
const [removeNode] = editorNode(slateEditor, removePath)
const removeEntry = getNode(
{
schema: slateEditor.schema,
editableTypes: slateEditor.editableTypes,
},
slateEditor,
removePath,
)
if (!removeEntry) {
return
}
const [removeNode] = removeEntry
slateEditor.apply({
type: 'remove_node',
path: removePath,
Expand Down Expand Up @@ -645,7 +656,18 @@ function removeExtraBlocks({

if (value.length < childrenLength) {
for (let i = childrenLength - 1; i > value.length - 1; i--) {
const [removeNode] = editorNode(slateEditor, [i])
const removeEntry2 = getNode(
{
schema: slateEditor.schema,
editableTypes: slateEditor.editableTypes,
},
slateEditor,
[i],
)
if (!removeEntry2) {
continue
}
const [removeNode] = removeEntry2
slateEditor.apply({
type: 'remove_node',
path: [i],
Expand Down Expand Up @@ -861,16 +883,24 @@ function replaceBlock({
applyDeselect(slateEditor)
}

const [oldNode] = editorNode(slateEditor, [index])
const oldNodeEntry = getNode(
{schema: slateEditor.schema, editableTypes: slateEditor.editableTypes},
slateEditor,
[index],
)
if (!oldNodeEntry) {
return
}
const [oldNode] = oldNodeEntry
slateEditor.apply({type: 'remove_node', path: [index], node: oldNode})
slateEditor.apply({type: 'insert_node', path: [index], node: slateBlock})

slateEditor.onChange()

if (
selectionFocusOnBlock &&
hasNode(slateEditor, currentSelection.anchor.path, slateEditor.schema) &&
hasNode(slateEditor, currentSelection.focus.path, slateEditor.schema)
hasNode(slateEditor, slateEditor, currentSelection.anchor.path) &&
hasNode(slateEditor, slateEditor, currentSelection.focus.path)
) {
applySelect(slateEditor, currentSelection)
}
Expand Down Expand Up @@ -945,7 +975,18 @@ function updateBlock({
if (childIndex > 0) {
debug.syncValue('Removing child')

const [childNode] = editorNode(slateEditor, [index, childIndex])
const childNodeEntry = getNode(
{
schema: slateEditor.schema,
editableTypes: slateEditor.editableTypes,
},
slateEditor,
[index, childIndex],
)
if (!childNodeEntry) {
return
}
const [childNode] = childNodeEntry
slateEditor.apply({
type: 'remove_node',
path: [index, childIndex],
Expand Down Expand Up @@ -1019,10 +1060,18 @@ function updateBlock({
} else if (oldBlockChild) {
debug.syncValue('Replacing child', currentBlockChild)

const [oldChild] = editorNode(slateEditor, [
index,
currentBlockChildIndex,
])
const oldChildEntry = getNode(
{
schema: slateEditor.schema,
editableTypes: slateEditor.editableTypes,
},
slateEditor,
[index, currentBlockChildIndex],
)
if (!oldChildEntry) {
return
}
const [oldChild] = oldChildEntry
slateEditor.apply({
type: 'remove_node',
path: [index, currentBlockChildIndex],
Expand Down
10 changes: 8 additions & 2 deletions packages/editor/src/internal-utils/apply-insert-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {isSpan} from '@portabletext/schema'
import {getNode} from '../node-traversal/get-node'
import {end} from '../slate/editor/end'
import {isEdge} from '../slate/editor/is-edge'
import {isEnd} from '../slate/editor/is-end'
Expand All @@ -9,7 +10,6 @@ import type {Node} from '../slate/interfaces/node'
import type {Path} from '../slate/interfaces/path'
import type {Point} from '../slate/interfaces/point'
import {extractProps} from '../slate/node/extract-props'
import {getNode} from '../slate/node/get-node'
import {isObjectNode} from '../slate/node/is-object-node'
import {nextPath} from '../slate/path/next-path'
import type {PortableTextSlateEditor} from '../types/slate-editor'
Expand Down Expand Up @@ -67,7 +67,13 @@ export function applyInsertNodeAtPoint(

// Split the node at the point if we're not at an edge
if (!isEdge(editor, at, matchPath)) {
const textNode = getNode(editor, at.path, editor.schema)
const textNodeEntry = getNode(editor, editor, at.path)

if (!textNodeEntry) {
return
}

const [textNode] = textNodeEntry
const properties = extractProps(textNode, editor.schema)

applySplitNode(editor, at.path, at.offset, properties)
Expand Down
10 changes: 8 additions & 2 deletions packages/editor/src/internal-utils/apply-merge-node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {isSpan, isTextBlock} from '@portabletext/schema'
import {getNode} from '../node-traversal/get-node'
import {withoutNormalizing} from '../slate/editor/without-normalizing'
import type {Path} from '../slate/interfaces/path'
import type {Point} from '../slate/interfaces/point'
import type {Range} from '../slate/interfaces/range'
import {getNode} from '../slate/node/get-node'
import {isAncestorPath} from '../slate/path/is-ancestor-path'
import {pathEndsBefore} from '../slate/path/path-ends-before'
import {pathEquals} from '../slate/path/path-equals'
Expand All @@ -28,7 +28,13 @@ export function applyMergeNode(
path: Path,
position: number,
): void {
const node = getNode(editor, path, editor.schema)
const nodeEntry = getNode(editor, editor, path)

if (!nodeEntry) {
return
}

const [node] = nodeEntry
const prevPath = previousPath(path)

// Pre-transform all refs with merge semantics
Expand Down
17 changes: 12 additions & 5 deletions packages/editor/src/internal-utils/apply-set-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {isTextBlock} from '@portabletext/schema'
import {getNode} from '../node-traversal/get-node'
import type {Path} from '../slate/interfaces/path'
import {getNode} from '../slate/node/get-node'
import {isObjectNode} from '../slate/node/is-object-node'
import type {PortableTextSlateEditor} from '../types/slate-editor'

Expand All @@ -19,7 +19,14 @@ export function applySetNode(
props: Record<string, unknown> | object,
path: Path,
): void {
const node = getNode(editor, path, editor.schema) as Record<string, unknown>
const nodeEntry = getNode(editor, editor, path)

if (!nodeEntry) {
return
}

const [node] = nodeEntry
const nodeRecord = node as Record<string, unknown>
const propsRecord = props as Record<string, unknown>
const properties: Record<string, unknown> = {}
const newProperties: Record<string, unknown> = {}
Expand All @@ -37,9 +44,9 @@ export function applySetNode(
continue
}

if (propsRecord[key] !== node[key]) {
if (node.hasOwnProperty(key)) {
properties[key] = node[key]
if (propsRecord[key] !== nodeRecord[key]) {
if (nodeRecord.hasOwnProperty(key)) {
properties[key] = nodeRecord[key]
}

if (propsRecord[key] != null) {
Expand Down
10 changes: 8 additions & 2 deletions packages/editor/src/internal-utils/apply-split-node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {isSpan, isTextBlock} from '@portabletext/schema'
import {getNode} from '../node-traversal/get-node'
import {withoutNormalizing} from '../slate/editor/without-normalizing'
import type {Node} from '../slate/interfaces/node'
import type {Path} from '../slate/interfaces/path'
import type {Point} from '../slate/interfaces/point'
import {getNode} from '../slate/node/get-node'
import {isAncestorPath} from '../slate/path/is-ancestor-path'
import {nextPath} from '../slate/path/next-path'
import {pathEndsBefore} from '../slate/path/path-ends-before'
Expand All @@ -26,7 +26,13 @@ export function applySplitNode(
position: number,
properties: Record<string, unknown>,
): void {
const node = getNode(editor, path, editor.schema)
const nodeEntry = getNode(editor, editor, path)

if (!nodeEntry) {
return
}

const [node] = nodeEntry

// Pre-transform all refs with split semantics
for (const ref of editor.pathRefs) {
Expand Down
Loading
Loading