Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"@portabletext/patches": "workspace:^",
"@portabletext/schema": "workspace:^",
"@portabletext/to-html": "^5.0.2",
"@sanity/json-match": "^1.0.5",
"@xstate/react": "^6.1.0",
"debug": "^4.4.3",
"scroll-into-view-if-needed": "^3.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/editor/src/behaviors/behavior.abstract.insert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {isTextBlock} from '@portabletext/schema'
import {getIndexForKey} from '@sanity/json-match'
import type {EditorSelector} from '../editor/editor-selector'
import {isSelectionExpanded} from '../selectors'
import {getFocusInlineObject} from '../selectors/selector.get-focus-inline-object'
Expand All @@ -21,7 +22,7 @@ function getUniqueBlockKey(
return snapshot.context.keyGenerator()
}

if (snapshot.blockIndexMap.has(blockKey)) {
if (getIndexForKey(snapshot.context.value, blockKey) !== undefined) {
return snapshot.context.keyGenerator()
}

Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/editor/Editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ export const PortableTextEditable = forwardRef<
value: slateEditor.children as Array<PortableTextBlock>,
selection: normalizedSelection,
},
blockIndexMap: slateEditor.blockIndexMap,
})
if (slateRange) {
slateEditor.select(slateRange)
Expand Down
5 changes: 2 additions & 3 deletions packages/editor/src/editor/create-editable-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type PortableTextChild,
type PortableTextObject,
} from '@portabletext/schema'
import {getIndexForKey} from '@sanity/json-match'
import {
isListItemActive,
isStyleActive,
Expand Down Expand Up @@ -249,7 +250,7 @@ export function createEditableAPI(
return [undefined, undefined]
}

const blockIndex = editor.blockIndexMap.get(blockKey)
const blockIndex = getIndexForKey(editor.children as Array<PortableTextBlock>, blockKey)

if (blockIndex === undefined) {
return [undefined, undefined]
Expand Down Expand Up @@ -502,15 +503,13 @@ export function createEditableAPI(
value: editor.children as Array<PortableTextBlock>,
selection: selectionA,
},
blockIndexMap: editor.blockIndexMap,
})
const rangeB = toSlateRange({
context: {
schema: editorActor.getSnapshot().context.schema,
value: editor.children as Array<PortableTextBlock>,
selection: selectionB,
},
blockIndexMap: editor.blockIndexMap,
})

// Make sure the ranges are valid
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/editor/editor-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export function getEditorSnapshot({
: null

return {
blockIndexMap: slateEditorInstance.blockIndexMap,
context: {
converters: [...editorActorSnapshot.context.converters],
keyGenerator: editorActorSnapshot.context.keyGenerator,
Expand Down
2 changes: 0 additions & 2 deletions packages/editor/src/editor/editor-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export type EditorContext = {
*/
export type EditorSnapshot = {
context: EditorContext
blockIndexMap: Map<string, number>
/**
* @beta
* Subject to change
Expand Down Expand Up @@ -61,7 +60,6 @@ export function createEditorSnapshot({
} satisfies EditorContext

return {
blockIndexMap: editor.blockIndexMap,
context,
decoratorState: editor.decoratorState,
} satisfies EditorSnapshot
Expand Down
3 changes: 0 additions & 3 deletions packages/editor/src/editor/range-decorations-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ export const rangeDecorationsMachine = setup({
value: context.slateEditor.children as Array<PortableTextBlock>,
selection: rangeDecoration.selection,
},
blockIndexMap: context.slateEditor.blockIndexMap,
})

if (!Range.isRange(slateRange)) {
Expand Down Expand Up @@ -134,7 +133,6 @@ export const rangeDecorationsMachine = setup({
value: context.slateEditor.children as Array<PortableTextBlock>,
selection: rangeDecoration.selection,
},
blockIndexMap: context.slateEditor.blockIndexMap,
})

if (!Range.isRange(slateRange)) {
Expand Down Expand Up @@ -169,7 +167,6 @@ export const rangeDecorationsMachine = setup({
value: context.slateEditor.children as Array<PortableTextBlock>,
selection: decoratedRange.rangeDecoration.selection,
},
blockIndexMap: context.slateEditor.blockIndexMap,
})

if (!Range.isRange(slateRange)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/editor/src/editor/render.element.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {PortableTextBlock} from '@portabletext/schema'
import {isTextBlock} from '@portabletext/schema'
import {getIndexForKey} from '@sanity/json-match'
import {useSelector} from '@xstate/react'
import {useContext, type ReactElement} from 'react'
import type {DropPosition} from '../behaviors/behavior.core.drop-position'
Expand Down Expand Up @@ -48,7 +49,7 @@ export function RenderElement(props: {
)
}

const blockIndex = slateStatic.blockIndexMap.get(props.element._key)
const blockIndex = getIndexForKey(slateStatic.children as Array<PortableTextBlock>, props.element._key)
const block =
blockIndex !== undefined
? (slateStatic.children as Array<PortableTextBlock>).at(blockIndex)
Expand Down
12 changes: 7 additions & 5 deletions packages/editor/src/editor/selection-state-context.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {getIndexForKey} from '@sanity/json-match'
import {useSelector} from '@xstate/react'
import {createContext, useContext} from 'react'
import {getFocusChild} from '../selectors'
Expand Down Expand Up @@ -111,17 +112,18 @@ export function SelectionStateProvider({
let selectedBlockKeys: Set<string> = emptySet

if (startBlockKey && endBlockKey) {
const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey)
const endBlockIndex = snapshot.blockIndexMap.get(endBlockKey)
const startBlockIndex = getIndexForKey(snapshot.context.value, startBlockKey)
const endBlockIndex = getIndexForKey(snapshot.context.value, endBlockKey)

if (startBlockIndex !== undefined && endBlockIndex !== undefined) {
const minIndex = Math.min(startBlockIndex, endBlockIndex)
const maxIndex = Math.max(startBlockIndex, endBlockIndex)
selectedBlockKeys = new Set<string>()

for (const [key, index] of snapshot.blockIndexMap) {
if (index >= minIndex && index <= maxIndex) {
selectedBlockKeys.add(key)
for (const block of snapshot.context.value) {
const index = getIndexForKey(snapshot.context.value, block._key)
if (index !== undefined && index >= minIndex && index <= maxIndex) {
selectedBlockKeys.add(block._key)
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions packages/editor/src/internal-utils/create-test-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ export function createTestSnapshot(snapshot: {
value: snapshot.context?.value ?? [],
selection: snapshot.context?.selection ?? null,
}
const blockIndexMap = new Map<string, number>()

snapshot.context?.value?.forEach((block, index) => {
blockIndexMap.set(block._key, index)
})

return {
blockIndexMap,
context,
decoratorState: snapshot?.decoratorState ?? {},
}
Expand Down
6 changes: 0 additions & 6 deletions packages/editor/src/internal-utils/to-slate-range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe(toSlateRange.name, () => {
},
},
},
blockIndexMap: new Map([[blockKey, 0]]),
})

expect(range).toEqual({
Expand Down Expand Up @@ -104,7 +103,6 @@ describe(toSlateRange.name, () => {
},
},
},
blockIndexMap: new Map([[blockKey, 0]]),
})

expect(range).toEqual({
Expand Down Expand Up @@ -137,7 +135,6 @@ describe(toSlateRange.name, () => {
},
},
},
blockIndexMap: new Map([[blockObjectKey, 0]]),
})

expect(range).toEqual({
Expand Down Expand Up @@ -179,7 +176,6 @@ describe(toSlateRange.name, () => {
},
},
},
blockIndexMap: new Map([[blockKey, 0]]),
})

expect(range).toEqual({
Expand Down Expand Up @@ -220,7 +216,6 @@ describe(toSlateRange.name, () => {
},
},
},
blockIndexMap: new Map([[blockKey, 0]]),
})

expect(range).toEqual({
Expand Down Expand Up @@ -267,7 +262,6 @@ describe(toSlateRange.name, () => {
},
},
},
blockIndexMap: new Map([[blockKey, 0]]),
})

expect(range).toEqual({
Expand Down
15 changes: 7 additions & 8 deletions packages/editor/src/internal-utils/to-slate-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
type PortableTextObject,
type PortableTextSpan,
} from '@portabletext/schema'
import type {EditorContext, EditorSnapshot} from '../editor/editor-snapshot'
import {getIndexForKey} from '@sanity/json-match'
import type {EditorContext} from '../editor/editor-snapshot'
import type {Path, Range} from '../slate'
import type {EditorSelectionPoint} from '../types/editor'
import {blockOffsetToSpanSelectionPoint} from '../utils/util.block-offset'
Expand All @@ -14,11 +15,9 @@ import {
getChildKeyFromSelectionPoint,
} from '../utils/util.selection-point'

export function toSlateRange(
snapshot: {
context: Pick<EditorContext, 'schema' | 'value' | 'selection'>
} & Pick<EditorSnapshot, 'blockIndexMap'>,
): Range | null {
export function toSlateRange(snapshot: {
context: Pick<EditorContext, 'schema' | 'value' | 'selection'>
}): Range | null {
if (!snapshot.context.selection) {
return null
}
Expand Down Expand Up @@ -69,7 +68,7 @@ export function toSlateRange(
export function toSlateSelectionPoint(
snapshot: {
context: Pick<EditorContext, 'schema' | 'value'>
} & Pick<EditorSnapshot, 'blockIndexMap'>,
},
selectionPoint: EditorSelectionPoint,
direction: 'forward' | 'backward',
):
Expand All @@ -84,7 +83,7 @@ export function toSlateSelectionPoint(
return undefined
}

const blockIndex = snapshot.blockIndexMap.get(blockKey)
const blockIndex = getIndexForKey(snapshot.context.value, blockKey)

if (blockIndex === undefined) {
return undefined
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/operations/operation.annotation.add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const addAnnotationOperationImplementation: OperationImplementation<
value: operation.editor.children as Array<PortableTextBlock>,
selection: operation.at,
},
blockIndexMap: operation.editor.blockIndexMap,
})
: null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const removeAnnotationOperationImplementation: OperationImplementation<
value: operation.editor.children as Array<PortableTextBlock>,
selection: operation.at,
},
blockIndexMap: operation.editor.blockIndexMap,
})
: null

Expand Down
5 changes: 3 additions & 2 deletions packages/editor/src/operations/operation.block.set.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {applyAll, set} from '@portabletext/patches'
import {isTextBlock} from '@portabletext/schema'
import {isTextBlock, type PortableTextBlock} from '@portabletext/schema'
import {getIndexForKey} from '@sanity/json-match'
import {applySetNode} from '../internal-utils/apply-set-node'
import {parseMarkDefs} from '../utils/parse-blocks'
import type {OperationImplementation} from './operation.types'

export const blockSetOperationImplementation: OperationImplementation<
'block.set'
> = ({context, operation}) => {
const blockIndex = operation.editor.blockIndexMap.get(operation.at[0]._key)
const blockIndex = getIndexForKey(operation.editor.children as Array<PortableTextBlock>, operation.at[0]._key)

if (blockIndex === undefined) {
throw new Error(
Expand Down
5 changes: 3 additions & 2 deletions packages/editor/src/operations/operation.block.unset.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {isTextBlock} from '@portabletext/schema'
import {isTextBlock, type PortableTextBlock} from '@portabletext/schema'
import {getIndexForKey} from '@sanity/json-match'
import {applySetNode} from '../internal-utils/apply-set-node'
import type {OperationImplementation} from './operation.types'

export const blockUnsetOperationImplementation: OperationImplementation<
'block.unset'
> = ({context, operation}) => {
const blockKey = operation.at[0]._key
const blockIndex = operation.editor.blockIndexMap.get(blockKey)
const blockIndex = getIndexForKey(operation.editor.children as Array<PortableTextBlock>, blockKey)

if (blockIndex === undefined) {
throw new Error(`Unable to find block index for block key ${blockKey}`)
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/operations/operation.child.set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const childSetOperationImplementation: OperationImplementation<
focus: {path: operation.at, offset: 0},
},
},
blockIndexMap: operation.editor.blockIndexMap,
})

if (!location) {
Expand Down
3 changes: 2 additions & 1 deletion packages/editor/src/operations/operation.child.unset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {PortableTextBlock} from '@portabletext/schema'
import {isTextBlock} from '@portabletext/schema'
import {getIndexForKey} from '@sanity/json-match'
import {applySetNode} from '../internal-utils/apply-set-node'
import {Editor} from '../slate'
import type {OperationImplementation} from './operation.types'
Expand All @@ -8,7 +9,7 @@ export const childUnsetOperationImplementation: OperationImplementation<
'child.unset'
> = ({context, operation}) => {
const blockKey = operation.at[0]._key
const blockIndex = operation.editor.blockIndexMap.get(blockKey)
const blockIndex = getIndexForKey(operation.editor.children as Array<PortableTextBlock>, blockKey)

if (blockIndex === undefined) {
throw new Error(`Unable to find block index for block key ${blockKey}`)
Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/operations/operation.decorator.add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const decoratorAddOperationImplementation: OperationImplementation<
value: operation.editor.children as Array<PortableTextBlock>,
selection: operation.at,
},
blockIndexMap: operation.editor.blockIndexMap,
})
: operation.editor.selection

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const decoratorRemoveOperationImplementation: OperationImplementation<
value: operation.editor.children as Array<PortableTextBlock>,
selection: operation.at,
},
blockIndexMap: operation.editor.blockIndexMap,
})
: editor.selection

Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/operations/operation.delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const deleteOperationImplementation: OperationImplementation<
value: operation.editor.children as Array<PortableTextBlock>,
selection: operation.at,
},
blockIndexMap: operation.editor.blockIndexMap,
})
: operation.editor.selection

Expand Down
1 change: 0 additions & 1 deletion packages/editor/src/operations/operation.insert.block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ export function insertBlock(options: {
value: editor.children as Array<PortableTextBlock>,
selection: options.at,
},
blockIndexMap: editor.blockIndexMap,
})
: editor.selection

Expand Down
10 changes: 7 additions & 3 deletions packages/editor/src/operations/operation.move.block.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type {PortableTextBlock} from '@portabletext/schema'
import {getIndexForKey} from '@sanity/json-match'
import {applyMoveNode} from '../internal-utils/apply-move-node'
import {getBlockKeyFromSelectionPoint} from '../utils/util.selection-point'
import type {OperationImplementation} from './operation.types'
Expand All @@ -14,7 +16,7 @@ export const moveBlockOperationImplementation: OperationImplementation<
throw new Error('Failed to get block key from selection point')
}

const originBlockIndex = operation.editor.blockIndexMap.get(originKey)
const originBlockIndex = getIndexForKey(operation.editor.children as Array<PortableTextBlock>, originKey)

if (originBlockIndex === undefined) {
throw new Error('Failed to get block index from block key')
Expand All @@ -29,8 +31,10 @@ export const moveBlockOperationImplementation: OperationImplementation<
throw new Error('Failed to get block key from selection point')
}

const destinationBlockIndex =
operation.editor.blockIndexMap.get(destinationKey)
const destinationBlockIndex = getIndexForKey(
operation.editor.children as Array<PortableTextBlock>,
destinationKey,
)

if (destinationBlockIndex === undefined) {
throw new Error('Failed to get block index from block key')
Expand Down
Loading
Loading