diff --git a/packages/@sanity/portable-text-editor/src/editor/Editable.tsx b/packages/@sanity/portable-text-editor/src/editor/Editable.tsx index 667153ef429..908aa6dec17 100644 --- a/packages/@sanity/portable-text-editor/src/editor/Editable.tsx +++ b/packages/@sanity/portable-text-editor/src/editor/Editable.tsx @@ -106,18 +106,18 @@ export const PortableTextEditable = forwardRef(function PortableTextEditable( const readOnly = usePortableTextEditorReadOnlyStatus() const ref = useForwardedRef(forwardedRef) - const {change$, keyGenerator, types, slateInstance: slateEditor} = portableTextEditor + const {change$, keyGenerator, schemaTypes, slateInstance: slateEditor} = portableTextEditor - const blockTypeName = types.block.name + const blockTypeName = schemaTypes.block.name // React/UI-spesific plugins const withInsertData = useMemo( - () => createWithInsertData(change$, types, keyGenerator), - [change$, keyGenerator, types] + () => createWithInsertData(change$, schemaTypes, keyGenerator), + [change$, keyGenerator, schemaTypes] ) const withHotKeys = useMemo( - () => createWithHotkeys(types, keyGenerator, portableTextEditor, hotkeys), - [hotkeys, keyGenerator, portableTextEditor, types] + () => createWithHotkeys(schemaTypes, keyGenerator, portableTextEditor, hotkeys), + [hotkeys, keyGenerator, portableTextEditor, schemaTypes] ) // Output a minimal React editor inside Editable when in readOnly mode. @@ -136,7 +136,7 @@ export const PortableTextEditable = forwardRef(function PortableTextEditable( (eProps: RenderElementProps) => ( ), - [types, spellCheck, readOnly, renderBlock, renderChild, renderListItem, renderStyle] + [schemaTypes, spellCheck, readOnly, renderBlock, renderChild, renderListItem, renderStyle] ) const renderLeaf = useCallback( @@ -159,7 +159,7 @@ export const PortableTextEditable = forwardRef(function PortableTextEditable( { @@ -319,7 +319,7 @@ export const PortableTextEditable = forwardRef(function PortableTextEditable( }, [portableTextEditor, scrollSelectionIntoView]) const decorate = useCallback(() => { - if (isEqualToEmptyEditor(slateEditor.children, types)) { + if (isEqualToEmptyEditor(slateEditor.children, schemaTypes)) { return [ { anchor: { @@ -335,7 +335,7 @@ export const PortableTextEditable = forwardRef(function PortableTextEditable( ] } return EMPTY_DECORATORS - }, [types, slateEditor.children]) + }, [slateEditor.children]) // The editor const slateEditable = useMemo( diff --git a/packages/@sanity/portable-text-editor/src/editor/Element.tsx b/packages/@sanity/portable-text-editor/src/editor/Element.tsx index d675adf3c8c..ec7d04d051c 100644 --- a/packages/@sanity/portable-text-editor/src/editor/Element.tsx +++ b/packages/@sanity/portable-text-editor/src/editor/Element.tsx @@ -4,7 +4,7 @@ import {Path, PortableTextChild, PortableTextObject, PortableTextTextBlock} from import {useSelected, useSlateStatic, ReactEditor, RenderElementProps} from '@sanity/slate-react' import { BlockRenderProps, - PortableTextMemberTypes, + PortableTextMemberSchemaTypes, RenderBlockFunction, RenderChildFunction, RenderListItemFunction, @@ -25,7 +25,7 @@ interface ElementProps { attributes: RenderElementProps['attributes'] children: ReactElement element: SlateElement - types: PortableTextMemberTypes + schemaTypes: PortableTextMemberSchemaTypes readOnly: boolean renderBlock?: RenderBlockFunction renderChild?: RenderChildFunction @@ -41,7 +41,7 @@ export const Element: FunctionComponent = ({ attributes, children, element, - types, + schemaTypes, readOnly, renderBlock, renderChild, @@ -56,8 +56,8 @@ export const Element: FunctionComponent = ({ const focused = (selected && editor.selection && Range.isCollapsed(editor.selection)) || false const value = useMemo( - () => fromSlateValue([element], types.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0], - [editor, element, types.block.name] + () => fromSlateValue([element], schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0], + [editor, element, schemaTypes.block.name] ) let renderedBlock = children @@ -78,8 +78,8 @@ export const Element: FunctionComponent = ({ if (editor.isInline(element)) { const path = ReactEditor.findPath(editor, element) const [block] = Editor.node(editor, path, {depth: 1}) - const type = types.inlineObjects.find((_type) => _type.name === element._type) - if (!type) { + const schemaType = schemaTypes.inlineObjects.find((_type) => _type.name === element._type) + if (!schemaType) { throw new Error('Could not find type for inline block element') } if (SlateElement.isElement(block)) { @@ -104,7 +104,7 @@ export const Element: FunctionComponent = ({ annotations: EMPTY_ANNOTATIONS, // These inline objects currently doesn't support annotations. This is a limitation of the current PT spec/model. children: , value: value as PortableTextChild, - type, + schemaType, focused, selected, path: elmPath, @@ -121,7 +121,7 @@ export const Element: FunctionComponent = ({ // If not inline, it's either a block (text) or a block object (non-text) // NOTE: text blocks aren't draggable with DraggableBlock (yet?) - if (element._type === types.block.name) { + if (element._type === schemaTypes.block.name) { className = `pt-block pt-text-block` const isListItem = 'listItem' in element if (debugRenders) { @@ -129,7 +129,7 @@ export const Element: FunctionComponent = ({ } const style = ('style' in element && element.style) || 'normal' className = `pt-block pt-text-block pt-text-block-style-${style}` - const blockStyleType = types.styles.find((item) => item.value === style) + const blockStyleType = schemaTypes.styles.find((item) => item.value === style) if (renderStyle && blockStyleType) { renderedBlock = renderStyle({ block: element as PortableTextTextBlock, @@ -150,7 +150,7 @@ export const Element: FunctionComponent = ({ className += ` pt-list-item pt-list-item-${element.listItem} pt-list-item-level-${level || 1}` } if (editor.isListBlock(value) && isListItem && element.listItem) { - const listType = types.lists.find((item) => item.value === element.listItem) + const listType = schemaTypes.lists.find((item) => item.value === element.listItem) if (renderListItem && listType) { renderedBlock = renderListItem({ block: value, @@ -166,7 +166,7 @@ export const Element: FunctionComponent = ({ } else { renderedBlock = ( {renderedBlock} @@ -183,7 +183,7 @@ export const Element: FunctionComponent = ({ path: blockPath, selected, style, - type: types.block, + type: schemaTypes.block, value, } @@ -196,7 +196,7 @@ export const Element: FunctionComponent = ({ ) } - const type = types.blockObjects.find((_type) => _type.name === element._type) + const type = schemaTypes.blockObjects.find((_type) => _type.name === element._type) if (!type) { throw new Error(`Could not find schema type for block element of _type ${element._type}`) } @@ -204,7 +204,11 @@ export const Element: FunctionComponent = ({ debug(`Render ${element._key} (object block)`) } className = 'pt-block pt-object-block' - const block = fromSlateValue([element], types.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0] + const block = fromSlateValue( + [element], + schemaTypes.block.name, + KEY_TO_VALUE_ELEMENT.get(editor) + )[0] const renderedBlockFromProps = renderBlock && renderBlock({ diff --git a/packages/@sanity/portable-text-editor/src/editor/Leaf.tsx b/packages/@sanity/portable-text-editor/src/editor/Leaf.tsx index 205b0da0b84..2f89a152c2a 100644 --- a/packages/@sanity/portable-text-editor/src/editor/Leaf.tsx +++ b/packages/@sanity/portable-text-editor/src/editor/Leaf.tsx @@ -5,7 +5,7 @@ import {uniq} from 'lodash' import {PortableTextObject, PortableTextTextBlock} from '@sanity/types' import { RenderChildFunction, - PortableTextMemberTypes, + PortableTextMemberSchemaTypes, RenderAnnotationFunction, RenderDecoratorFunction, } from '../types/editor' @@ -19,7 +19,7 @@ const debugRenders = false interface LeafProps extends RenderLeafProps { children: ReactElement keyGenerator: () => string - types: PortableTextMemberTypes + schemaTypes: PortableTextMemberSchemaTypes renderAnnotation?: RenderAnnotationFunction renderChild?: RenderChildFunction renderDecorator?: RenderDecoratorFunction @@ -29,21 +29,21 @@ interface LeafProps extends RenderLeafProps { export const Leaf = (props: LeafProps) => { const editor = useSlateStatic() const selected = useSelected() - const {attributes, children, leaf, types, keyGenerator, renderChild, readOnly} = props + const {attributes, children, leaf, schemaTypes, keyGenerator, renderChild, readOnly} = props const spanRef = React.useRef(null) let returnedChildren = children const focused = (selected && editor.selection && Range.isCollapsed(editor.selection)) || false // Render text nodes - if (Text.isText(leaf) && leaf._type === types.span.name) { + if (Text.isText(leaf) && leaf._type === schemaTypes.span.name) { const block = children.props.parent as PortableTextTextBlock | undefined const path = block ? [{_key: block._key}, 'children', {_key: leaf._key}] : [] - const decoratorValues = types.decorators.map((dec) => dec.value) + const decoratorValues = schemaTypes.decorators.map((dec) => dec.value) const marks: string[] = uniq( (Array.isArray(leaf.marks) ? leaf.marks : []).filter((mark) => decoratorValues.includes(mark)) ) marks.forEach((mark) => { - const type = types.decorators.find((dec) => dec.value === mark) + const type = schemaTypes.decorators.find((dec) => dec.value === mark) if (type && props.renderDecorator) { returnedChildren = props.renderDecorator({ children: returnedChildren, @@ -69,7 +69,7 @@ export const Leaf = (props: LeafProps) => { if (block && annotations.length > 0) { annotations.forEach((annotation) => { - const type = types.annotations.find((t) => t.name === annotation._type) + const type = schemaTypes.annotations.find((t) => t.name === annotation._type) if (type) { if (props.renderAnnotation) { returnedChildren = ( @@ -103,7 +103,7 @@ export const Leaf = (props: LeafProps) => { returnedChildren = renderChild({ children: defaultRendered, value: child, - type: types.span, + schemaType: schemaTypes.span, focused, selected, path, diff --git a/packages/@sanity/portable-text-editor/src/editor/PortableTextEditor.tsx b/packages/@sanity/portable-text-editor/src/editor/PortableTextEditor.tsx index f172fae29de..d86f59e7e36 100644 --- a/packages/@sanity/portable-text-editor/src/editor/PortableTextEditor.tsx +++ b/packages/@sanity/portable-text-editor/src/editor/PortableTextEditor.tsx @@ -17,7 +17,7 @@ import {createEditor, Descendant, Transforms} from 'slate' import {debounce, isEqual, throttle} from 'lodash' import {Slate, withReact} from '@sanity/slate-react' import {compileType} from '../utils/schema' -import {getPortableTextMemberTypes} from '../utils/getPortableTextMemberTypes' +import {getPortableTextMemberSchemaTypes} from '../utils/getPortableTextMemberSchemaTypes' import type {Patch} from '../types/patch' import { EditorSelection, @@ -28,7 +28,7 @@ import { PatchObservable, PortableTextSlateEditor, EditableAPIDeleteOptions, - PortableTextMemberTypes, + PortableTextMemberSchemaTypes, } from '../types/editor' import {validateValue} from '../utils/validateValue' import {debugWithName} from '../utils/debug' @@ -64,7 +64,7 @@ export type PortableTextEditorProps = PropsWithChildren<{ /** * Schema type for the portable text field */ - type: ArraySchemaType | ArrayDefinition + schemaType: ArraySchemaType | ArrayDefinition /** * Maximum number of blocks to allow within the editor @@ -109,7 +109,7 @@ export class PortableTextEditor extends React.Component< public change$: EditorChanges = new Subject() public keyGenerator: () => string public maxBlocks: number | undefined - public types: PortableTextMemberTypes + public schemaTypes: PortableTextMemberSchemaTypes public readOnly: boolean public slateInstance: PortableTextSlateEditor public type: ArraySchemaType @@ -124,7 +124,7 @@ export class PortableTextEditor extends React.Component< constructor(props: PortableTextEditorProps) { super(props) - if (!props.type) { + if (!props.schemaType) { throw new Error('PortableTextEditor: missing "type" property') } @@ -138,12 +138,14 @@ export class PortableTextEditor extends React.Component< } // Test if we have a compiled schema type, if not, conveniently compile it - this.type = props.type.hasOwnProperty('jsonType') ? props.type : compileType(props.type) + this.type = props.schemaType.hasOwnProperty('jsonType') + ? props.schemaType + : compileType(props.schemaType) // Indicate that we are loading this.change$.next({type: 'loading', isLoading: true}) // Get the block types feature set (lookup table) - this.types = getPortableTextMemberTypes(this.type) + this.schemaTypes = getPortableTextMemberSchemaTypes(this.type) // Setup keyGenerator (either from props, or default) this.keyGenerator = props.keyGenerator || defaultKeyGenerator @@ -198,7 +200,7 @@ export class PortableTextEditor extends React.Component< this.readOnly = Boolean(props.readOnly) || false // Validate the incoming value if (props.value) { - const validation = validateValue(props.value, this.types, this.keyGenerator) + const validation = validateValue(props.value, this.schemaTypes, this.keyGenerator) if (props.value && !validation.valid) { this.change$.next({type: 'loading', isLoading: false}) this.change$.next({ @@ -221,7 +223,7 @@ export class PortableTextEditor extends React.Component< getValueOrInitialValue(props.value, [ this.slateInstance.createPlaceholderBlock(), ] as PortableTextBlock[]), - {types: this.types}, + {schemaTypes: this.schemaTypes}, KEY_TO_SLATE_ELEMENT.get(this.slateInstance) ), } @@ -304,13 +306,13 @@ export class PortableTextEditor extends React.Component< return } // If the editor is empty and there is a new value, just set that value directly. - if (isEqualToEmptyEditor(this.slateInstance.children, this.types) && this.props.value) { + if (isEqualToEmptyEditor(this.slateInstance.children, this.schemaTypes) && this.props.value) { const oldSel = this.slateInstance.selection Transforms.deselect(this.slateInstance) this.slateInstance.children = toSlateValue( val, { - types: this.types, + schemaTypes: this.schemaTypes, }, KEY_TO_SLATE_ELEMENT.get(this.slateInstance) ) @@ -325,7 +327,7 @@ export class PortableTextEditor extends React.Component< const isEqualToValue = !(val || []).some((blk, index) => { const compareBlock = toSlateValue( [blk], - {types: this.types}, + {schemaTypes: this.schemaTypes}, KEY_TO_SLATE_ELEMENT.get(this.slateInstance) )[0] if (!isEqual(compareBlock, this.slateInstance.children[index])) { @@ -339,7 +341,7 @@ export class PortableTextEditor extends React.Component< } // Value is different - validate it. debug('Validating') - const validation = validateValue(val, this.types, this.keyGenerator) + const validation = validateValue(val, this.schemaTypes, this.keyGenerator) if (val && !validation.valid) { this.change$.next({ type: 'invalidValue', @@ -356,7 +358,7 @@ export class PortableTextEditor extends React.Component< const slateValueFromProps = toSlateValue( val, { - types: this.types, + schemaTypes: this.schemaTypes, }, KEY_TO_SLATE_ELEMENT.get(this.slateInstance) ) @@ -423,9 +425,6 @@ export class PortableTextEditor extends React.Component< static focusChild = (editor: PortableTextEditor): PortableTextChild | undefined => { return editor.editable?.focusChild() } - static getTypes = (editor: PortableTextEditor) => { - return editor.types - } static getSelection = (editor: PortableTextEditor) => { return editor.editable ? editor.editable.getSelection() : null } diff --git a/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditor.test.tsx b/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditor.test.tsx index c296d74732f..45e24a8f514 100644 --- a/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditor.test.tsx +++ b/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditor.test.tsx @@ -9,7 +9,7 @@ import {render, waitFor} from '@testing-library/react' import {PortableTextBlock} from '@sanity/types' import {PortableTextEditor} from '../PortableTextEditor' import {EditorSelection} from '../..' -import {PortableTextEditorTester, type} from './PortableTextEditorTester' +import {PortableTextEditorTester, schemaType} from './PortableTextEditorTester' const helloBlock: PortableTextBlock = { _key: '123', @@ -27,7 +27,7 @@ describe('initialization', () => { onChange={onChange} renderPlaceholder={() => 'Jot something down here'} ref={editorRef} - type={type} + schemaType={schemaType} value={undefined} /> ) @@ -94,7 +94,9 @@ describe('initialization', () => { it('takes value from props', () => { const initialValue = [helloBlock] const onChange = jest.fn() - render() + render( + + ) expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue}) }) it('takes initial selection from props', async () => { @@ -110,7 +112,7 @@ describe('initialization', () => { onChange={onChange} ref={editorRef} selection={initialSelection} - type={type} + schemaType={schemaType} value={initialValue} /> ) @@ -139,7 +141,7 @@ describe('initialization', () => { onChange={onChange} ref={editorRef} selection={initialSelection} - type={type} + schemaType={schemaType} value={initialValue} /> ) @@ -159,7 +161,7 @@ describe('initialization', () => { onChange={onChange} ref={editorRef} selection={newSelection} - type={type} + schemaType={schemaType} value={initialValue} /> ) diff --git a/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditorTester.tsx b/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditorTester.tsx index b4fccff39f3..f7d2333831e 100644 --- a/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditorTester.tsx +++ b/packages/@sanity/portable-text-editor/src/editor/__tests__/PortableTextEditorTester.tsx @@ -47,7 +47,7 @@ let key = 0 export const PortableTextEditorTester = forwardRef(function PortableTextEditorTester( props: Partial> & { - type: PortableTextEditorProps['type'] + schemaType: PortableTextEditorProps['schemaType'] value?: PortableTextEditorProps['value'] onChange?: PortableTextEditorProps['onChange'] selection?: PortableTextEditableProps['selection'] @@ -64,7 +64,7 @@ export const PortableTextEditorTester = forwardRef(function PortableTextEditorTe }, []) return ( { ) @@ -96,7 +96,7 @@ describe('plugin:withEditableAPI: .delete()', () => { ) diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/__tests__/withPortableTextMarkModelNormalization.test.tsx b/packages/@sanity/portable-text-editor/src/editor/plugins/__tests__/withPortableTextMarkModelNormalization.test.tsx index a80f0f3e97d..5d1ef7f9cbb 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/__tests__/withPortableTextMarkModelNormalization.test.tsx +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/__tests__/withPortableTextMarkModelNormalization.test.tsx @@ -6,9 +6,11 @@ import '@testing-library/jest-dom/extend-expect' import {render, waitFor} from '@testing-library/react' import React from 'react' -import {ObjectSchemaType} from '@sanity/types' import {PortableTextEditor} from '../../PortableTextEditor' -import {PortableTextEditorTester, type} from '../../../editor/__tests__/PortableTextEditorTester' +import { + PortableTextEditorTester, + schemaType, +} from '../../../editor/__tests__/PortableTextEditorTester' import {EditorSelection} from '../../../types/editor' describe('plugin:withPortableTextMarksModel: normalization', () => { @@ -81,7 +83,7 @@ describe('plugin:withPortableTextMarksModel: normalization', () => { ) @@ -93,7 +95,7 @@ describe('plugin:withPortableTextMarksModel: normalization', () => { anchor: {path: [{_key: '5fc57af23597'}, 'children', {_key: '11c8c9f783a8'}], offset: 0}, }) // eslint-disable-next-line max-nested-callbacks - const linkType = editorRef.current.types.annotations.find((a) => a.name === 'link') + const linkType = editorRef.current.schemaTypes.annotations.find((a) => a.name === 'link') if (!linkType) { throw new Error('No link type found') } @@ -184,7 +186,7 @@ describe('plugin:withPortableTextMarksModel: normalization', () => { ) @@ -264,7 +266,7 @@ describe('plugin:withPortableTextMarksModel: normalization', () => { ) @@ -407,7 +409,7 @@ describe('plugin:withPortableTextMarksModel: normalization', () => { ) @@ -523,7 +525,7 @@ describe('plugin:withPortableTextMarksModel: normalization', () => { ) @@ -633,7 +635,7 @@ describe('plugin:withPortableTextMarksModel: normalization', () => { ) diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithEditableAPI.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithEditableAPI.ts index 8c9b9292aaf..6bb63468b95 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithEditableAPI.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithEditableAPI.ts @@ -12,7 +12,7 @@ import {DOMNode} from '@sanity/slate-react/dist/utils/dom' import { EditableAPIDeleteOptions, EditorSelection, - PortableTextMemberTypes, + PortableTextMemberSchemaTypes, PortableTextSlateEditor, } from '../../types/editor' import {toSlateValue, fromSlateValue} from '../../utils/values' @@ -26,7 +26,7 @@ const debug = debugWithName('API:editable') export function createWithEditableAPI( portableTextEditor: PortableTextEditor, - types: PortableTextMemberTypes, + types: PortableTextMemberSchemaTypes, keyGenerator: () => string ) { return function withEditableAPI(editor: PortableTextSlateEditor): PortableTextSlateEditor { diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithHotKeys.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithHotKeys.ts index 9d7444abc76..7fbfe338235 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithHotKeys.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithHotKeys.ts @@ -4,7 +4,7 @@ import {Editor, Transforms, Path, Range} from 'slate' import isHotkey from 'is-hotkey' import {ReactEditor} from '@sanity/slate-react' import {isPortableTextSpan, isPortableTextTextBlock} from '@sanity/types' -import {PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import {PortableTextMemberSchemaTypes, PortableTextSlateEditor} from '../../types/editor' import {HotkeyOptions} from '../../types/options' import {debugWithName} from '../../utils/debug' import {toSlateValue} from '../../utils/values' @@ -27,7 +27,7 @@ const DEFAULT_HOTKEYS: HotkeyOptions = { * */ export function createWithHotkeys( - types: PortableTextMemberTypes, + types: PortableTextMemberSchemaTypes, keyGenerator: () => string, portableTextEditor: PortableTextEditor, hotkeysFromOptions?: HotkeyOptions diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithInsertData.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithInsertData.ts index 9c2afe0a79c..0ca7152432e 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithInsertData.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithInsertData.ts @@ -2,7 +2,11 @@ import {Node, Transforms, Editor, Descendant, Range} from 'slate' import {htmlToBlocks, normalizeBlock} from '@sanity/block-tools' import {ReactEditor} from '@sanity/slate-react' import {PortableTextBlock, PortableTextChild} from '@sanity/types' -import {EditorChanges, PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import { + EditorChanges, + PortableTextMemberSchemaTypes, + PortableTextSlateEditor, +} from '../../types/editor' import {fromSlateValue, toSlateValue} from '../../utils/values' import {validateValue} from '../../utils/validateValue' import {debugWithName} from '../../utils/debug' @@ -15,12 +19,12 @@ const debug = debugWithName('plugin:withInsertData') */ export function createWithInsertData( change$: EditorChanges, - types: PortableTextMemberTypes, + schemaTypes: PortableTextMemberSchemaTypes, keyGenerator: () => string ) { return function withInsertData(editor: PortableTextSlateEditor): PortableTextSlateEditor { - const blockTypeName = types.block.name - const spanTypeName = types.span.name + const blockTypeName = schemaTypes.block.name + const spanTypeName = schemaTypes.span.name const toPlainText = (blocks: PortableTextBlock[]) => { return blocks @@ -32,12 +36,14 @@ export function createWithInsertData( return child.text } return `[${ - types.inlineObjects.find((t) => t.name === child._type)?.title || 'Object' + schemaTypes.inlineObjects.find((t) => t.name === child._type)?.title || 'Object' }]` }) .join('') } - return `[${types.blockObjects.find((t) => t.name === block._type)?.title || 'Object'}]` + return `[${ + schemaTypes.blockObjects.find((t) => t.name === block._type)?.title || 'Object' + }]` }) .join('\n\n') } @@ -126,12 +132,12 @@ export function createWithInsertData( if (Array.isArray(parsed) && parsed.length > 0) { const slateValue = regenerateKeys( editor, - toSlateValue(parsed, {types}), + toSlateValue(parsed, {schemaTypes}), keyGenerator, spanTypeName ) // Validate the result - const validation = validateValue(parsed, types, keyGenerator) + const validation = validateValue(parsed, schemaTypes, keyGenerator) // Bail out if it's not valid if (!validation.valid) { const errorDescription = `${validation.resolution?.description}` @@ -169,10 +175,10 @@ export function createWithInsertData( let insertedType if (html) { - portableText = htmlToBlocks(html, types.portableText).map((block) => + portableText = htmlToBlocks(html, schemaTypes.portableText).map((block) => normalizeBlock(block, {blockTypeName}) ) as PortableTextBlock[] - fragment = toSlateValue(portableText, {types}) + fragment = toSlateValue(portableText, {schemaTypes}) insertedType = 'HTML' } else { // plain text @@ -183,17 +189,17 @@ export function createWithInsertData( ) .join('') const textToHtml = `${blocks}` - portableText = htmlToBlocks(textToHtml, types.portableText).map((block) => + portableText = htmlToBlocks(textToHtml, schemaTypes.portableText).map((block) => normalizeBlock(block, {blockTypeName}) ) as PortableTextBlock[] fragment = toSlateValue(portableText, { - types, + schemaTypes, }) insertedType = 'text' } // Validate the result - const validation = validateValue(portableText, types, keyGenerator) + const validation = validateValue(portableText, schemaTypes, keyGenerator) // Bail out if it's not valid if (!validation.valid) { diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithObjectKeys.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithObjectKeys.ts index 18a99f0f948..20fa2e2342f 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithObjectKeys.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithObjectKeys.ts @@ -1,12 +1,15 @@ import {Element, Transforms, Node, Editor} from 'slate' -import {PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import {PortableTextMemberSchemaTypes, PortableTextSlateEditor} from '../../types/editor' import {isPreservingKeys, PRESERVE_KEYS} from '../../utils/withPreserveKeys' /** * This plugin makes sure that every new node in the editor get a new _key prop when created * */ -export function createWithObjectKeys(types: PortableTextMemberTypes, keyGenerator: () => string) { +export function createWithObjectKeys( + schemaTypes: PortableTextMemberSchemaTypes, + keyGenerator: () => string +) { return function withKeys(editor: PortableTextSlateEditor): PortableTextSlateEditor { PRESERVE_KEYS.set(editor, false) const {apply, normalizeNode} = editor @@ -31,7 +34,7 @@ export function createWithObjectKeys(types: PortableTextMemberTypes, keyGenerato } editor.normalizeNode = (entry) => { const [node, path] = entry - if (Element.isElement(node) && node._type === types.block.name) { + if (Element.isElement(node) && node._type === schemaTypes.block.name) { // Set key on block itself if (!node._key) { Transforms.setNodes(editor, {_key: keyGenerator()}, {at: path}) diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPatches.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPatches.ts index 081305025f3..36d2c2736ab 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPatches.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPatches.ts @@ -19,7 +19,11 @@ import {insert, setIfMissing, unset} from '../../patch/PatchEvent' import type {Patch} from '../../types/patch' import {fromSlateValue, isEqualToEmptyEditor} from '../../utils/values' -import {EditorChange, PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import { + EditorChange, + PortableTextMemberSchemaTypes, + PortableTextSlateEditor, +} from '../../types/editor' import {debugWithName} from '../../utils/debug' import {PATCHING, isPatching, withoutPatching} from '../../utils/withoutPatching' import {KEY_TO_VALUE_ELEMENT} from '../../utils/weakMaps' @@ -76,7 +80,7 @@ export interface PatchFunctions { interface Options { patchFunctions: PatchFunctions change$: Subject - types: PortableTextMemberTypes + schemaTypes: PortableTextMemberSchemaTypes syncValue: () => void incomingPatches$?: Observable<{ patches: Patch[] @@ -87,7 +91,7 @@ interface Options { export function createWithPatches({ patchFunctions, change$, - types, + schemaTypes, syncValue, incomingPatches$, }: Options): [ @@ -98,7 +102,7 @@ export function createWithPatches({ // The editor.children would no longer contain that information if the node is already deleted. let previousChildren: Descendant[] - const patchToOperations = createPatchToOperations(types, defaultKeyGenerator) + const patchToOperations = createPatchToOperations(schemaTypes, defaultKeyGenerator) let patchSubscription: Subscription const cleanupFn = () => { if (patchSubscription) { @@ -160,12 +164,12 @@ export function createWithPatches({ // Update previous children here before we apply previousChildren = editor.children - const editorWasEmpty = isEqualToEmptyEditor(previousChildren, types) + const editorWasEmpty = isEqualToEmptyEditor(previousChildren, schemaTypes) // Apply the operation apply(operation) - const editorIsEmpty = isEqualToEmptyEditor(editor.children, types) + const editorIsEmpty = isEqualToEmptyEditor(editor.children, schemaTypes) if (!isPatching(editor)) { debug(`Editor is not producing patch for operation ${operation.type}`, operation) @@ -177,7 +181,7 @@ export function createWithPatches({ if (editorWasEmpty && operation.type !== 'set_selection') { patches.push(setIfMissing([], [])) previousChildren.forEach((c, index) => { - patches.push(insert(fromSlateValue([c], types.block.name), 'before', [index])) + patches.push(insert(fromSlateValue([c], schemaTypes.block.name), 'before', [index])) }) } switch (operation.type) { @@ -241,7 +245,7 @@ export function createWithPatches({ type: 'unset', previousValue: fromSlateValue( previousChildren, - types.block.name, + schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor) ), }) diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPlaceholderBlock.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPlaceholderBlock.ts index 956e90c8238..f7402ad998a 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPlaceholderBlock.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPlaceholderBlock.ts @@ -1,5 +1,5 @@ import {Transforms, Descendant} from 'slate' -import {PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import {PortableTextMemberSchemaTypes, PortableTextSlateEditor} from '../../types/editor' import {debugWithName} from '../../utils/debug' import {withoutPatching} from '../../utils/withoutPatching' import {withoutSaving} from './createWithUndoRedo' @@ -7,7 +7,7 @@ import {withoutSaving} from './createWithUndoRedo' const debug = debugWithName('plugin:withPlaceholderBlock') interface Options { - types: PortableTextMemberTypes + schemaTypes: PortableTextMemberSchemaTypes keyGenerator: () => string } /** @@ -15,15 +15,15 @@ interface Options { * */ export function createWithPlaceholderBlock({ - types, + schemaTypes, keyGenerator, }: Options): (editor: PortableTextSlateEditor) => PortableTextSlateEditor { return function withPlaceholderBlock(editor: PortableTextSlateEditor): PortableTextSlateEditor { editor.createPlaceholderBlock = (): Descendant => { return { - _type: types.block.name, + _type: schemaTypes.block.name, _key: keyGenerator(), - style: types.styles[0].value, + style: schemaTypes.styles[0].value || 'normal', markDefs: [], children: [ { diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextBlockStyle.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextBlockStyle.ts index 595d9062d77..5141cc46b84 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextBlockStyle.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextBlockStyle.ts @@ -1,6 +1,10 @@ import {Subject} from 'rxjs' import {Editor, Transforms, Element, Path, Text as SlateText, Node} from 'slate' -import {EditorChange, PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import { + EditorChange, + PortableTextMemberSchemaTypes, + PortableTextSlateEditor, +} from '../../types/editor' import {debugWithName} from '../../utils/debug' import {toPortableTextRange} from '../../utils/ranges' import {fromSlateValue} from '../../utils/values' @@ -8,7 +12,7 @@ import {fromSlateValue} from '../../utils/values' const debug = debugWithName('plugin:withPortableTextBlockStyle') export function createWithPortableTextBlockStyle( - types: PortableTextMemberTypes, + types: PortableTextMemberSchemaTypes, change$: Subject ): (editor: PortableTextSlateEditor) => PortableTextSlateEditor { const defaultStyle = types.styles[0].value diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextLists.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextLists.ts index a2663ffef95..baf4989f1ec 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextLists.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextLists.ts @@ -1,11 +1,11 @@ import {Editor, Transforms, Element, Text, Node} from 'slate' -import {PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import {PortableTextMemberSchemaTypes, PortableTextSlateEditor} from '../../types/editor' import {debugWithName} from '../../utils/debug' const debug = debugWithName('plugin:withPortableTextLists') const MAX_LIST_LEVEL = 10 -export function createWithPortableTextLists(types: PortableTextMemberTypes) { +export function createWithPortableTextLists(types: PortableTextMemberSchemaTypes) { return function withPortableTextLists(editor: PortableTextSlateEditor): PortableTextSlateEditor { editor.pteToggleListItem = (listItemStyle: string) => { const isActive = editor.pteHasListStyle(listItemStyle) diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextMarkModel.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextMarkModel.ts index f4fa1db518a..14e0dd6c989 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextMarkModel.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextMarkModel.ts @@ -9,12 +9,12 @@ import {isEqual, flatten, uniq} from 'lodash' import {Editor, Range, Transforms, Text, Path, NodeEntry, Element} from 'slate' import {debugWithName} from '../../utils/debug' -import {PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import {PortableTextMemberSchemaTypes, PortableTextSlateEditor} from '../../types/editor' const debug = debugWithName('plugin:withPortableTextMarkModel') export function createWithPortableTextMarkModel( - types: PortableTextMemberTypes + types: PortableTextMemberSchemaTypes ): (editor: PortableTextSlateEditor) => PortableTextSlateEditor { return function withPortableTextMarkModel(editor: PortableTextSlateEditor) { const {apply, normalizeNode} = editor diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextSelections.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextSelections.ts index 798cac7de40..60213eacee0 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextSelections.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithPortableTextSelections.ts @@ -2,7 +2,7 @@ import {Subject} from 'rxjs' import { EditorChange, EditorSelection, - PortableTextMemberTypes, + PortableTextMemberSchemaTypes, PortableTextSlateEditor, } from '../../types/editor' import {debugWithName} from '../../utils/debug' @@ -15,7 +15,7 @@ const debug = debugWithName('plugin:withPortableTextSelections') // This plugin will make sure that we emit a PT selection whenever the editor has changed. export function createWithPortableTextSelections( change$: Subject, - types: PortableTextMemberTypes + types: PortableTextMemberSchemaTypes ) { return function withPortableTextSelections( editor: PortableTextSlateEditor diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithSchemaTypes.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithSchemaTypes.ts index 9698edccf78..016e8e142dd 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithSchemaTypes.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithSchemaTypes.ts @@ -8,14 +8,14 @@ import { isPortableTextListBlock, } from '@sanity/types' import {debugWithName} from '../../utils/debug' -import {PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import {PortableTextMemberSchemaTypes, PortableTextSlateEditor} from '../../types/editor' const debug = debugWithName('plugin:withSchemaTypes') /** * This plugin makes sure that schema types are recognized properly by Slate as blocks, voids, inlines * */ -export function createWithSchemaTypes(types: PortableTextMemberTypes) { +export function createWithSchemaTypes(types: PortableTextMemberSchemaTypes) { return function withSchemaTypes(editor: PortableTextSlateEditor): PortableTextSlateEditor { editor.isTextBlock = (value: unknown): value is PortableTextTextBlock => { return isPortableTextTextBlock(value) && value._type === types.block.name diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithUtils.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithUtils.ts index 64e65af1b65..7fd5d1f9670 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/createWithUtils.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/createWithUtils.ts @@ -1,18 +1,18 @@ import {Editor, Range, Transforms, Text} from 'slate' -import {PortableTextMemberTypes, PortableTextSlateEditor} from '../../types/editor' +import {PortableTextMemberSchemaTypes, PortableTextSlateEditor} from '../../types/editor' import {debugWithName} from '../../utils/debug' const debug = debugWithName('plugin:withUtils') interface Options { - types: PortableTextMemberTypes + schemaTypes: PortableTextMemberSchemaTypes keyGenerator: () => string } /** * This plugin makes various util commands available in the editor * */ -export function createWithUtils({types, keyGenerator}: Options) { +export function createWithUtils({schemaTypes, keyGenerator}: Options) { return function withUtils(editor: PortableTextSlateEditor): PortableTextSlateEditor { // Expands the the selection to wrap around the word the focus is at editor.pteExpandToWord = () => { diff --git a/packages/@sanity/portable-text-editor/src/editor/plugins/index.ts b/packages/@sanity/portable-text-editor/src/editor/plugins/index.ts index 55677a6e267..4246bfddd34 100644 --- a/packages/@sanity/portable-text-editor/src/editor/plugins/index.ts +++ b/packages/@sanity/portable-text-editor/src/editor/plugins/index.ts @@ -43,7 +43,8 @@ export const withPlugins = ( ): PortableTextSlateEditor => { const e = editor as T & PortableTextSlateEditor const {portableTextEditor} = options - const {types, keyGenerator, readOnly, change$, syncValue, incomingPatches$} = portableTextEditor + const {schemaTypes, keyGenerator, readOnly, change$, syncValue, incomingPatches$} = + portableTextEditor e.maxBlocks = portableTextEditor.maxBlocks || -1 e.readOnly = portableTextEditor.readOnly || false if (e.destroy) { @@ -57,34 +58,34 @@ export const withPlugins = ( normalizeNode: e.normalizeNode, }) } - const operationToPatches = createOperationToPatches(types) - const withObjectKeys = createWithObjectKeys(types, keyGenerator) - const withSchemaTypes = createWithSchemaTypes(types) - const withEditableAPI = createWithEditableAPI(portableTextEditor, types, keyGenerator) + const operationToPatches = createOperationToPatches(schemaTypes) + const withObjectKeys = createWithObjectKeys(schemaTypes, keyGenerator) + const withSchemaTypes = createWithSchemaTypes(schemaTypes) + const withEditableAPI = createWithEditableAPI(portableTextEditor, schemaTypes, keyGenerator) const [withPatches, withPatchesCleanupFunction] = readOnly ? [] : createWithPatches({ patchFunctions: operationToPatches, change$, - types, + schemaTypes, syncValue, incomingPatches$, }) const withMaxBlocks = createWithMaxBlocks() - const withPortableTextLists = createWithPortableTextLists(types) + const withPortableTextLists = createWithPortableTextLists(schemaTypes) const [withUndoRedo, withUndoRedoCleanupFunction] = readOnly ? [] : createWithUndoRedo(incomingPatches$) - const withPortableTextMarkModel = createWithPortableTextMarkModel(types) - const withPortableTextBlockStyle = createWithPortableTextBlockStyle(types, change$) + const withPortableTextMarkModel = createWithPortableTextMarkModel(schemaTypes) + const withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes, change$) const withPlaceholderBlock = createWithPlaceholderBlock({ keyGenerator, - types, + schemaTypes, }) - const withUtils = createWithUtils({keyGenerator, types}) - const withPortableTextSelections = createWithPortableTextSelections(change$, types) + const withUtils = createWithUtils({keyGenerator, schemaTypes}) + const withPortableTextSelections = createWithPortableTextSelections(change$, schemaTypes) e.destroy = () => { const originalFunctions = originalFnMap.get(e) diff --git a/packages/@sanity/portable-text-editor/src/types/editor.ts b/packages/@sanity/portable-text-editor/src/types/editor.ts index 3b8282b652a..2f6c4081cd4 100644 --- a/packages/@sanity/portable-text-editor/src/types/editor.ts +++ b/packages/@sanity/portable-text-editor/src/types/editor.ts @@ -310,7 +310,7 @@ export type OnPasteResultOrPromise = OnPasteResult | Promise export type OnPasteFn = (arg0: { event: React.ClipboardEvent path: Path - types: PortableTextMemberTypes + schemaTypes: PortableTextMemberSchemaTypes value: PortableTextBlock[] | undefined }) => OnPasteResultOrPromise @@ -350,7 +350,7 @@ export interface BlockChildRenderProps { focused: boolean path: Path selected: boolean - type: ObjectSchemaType + schemaType: ObjectSchemaType value: PortableTextChild } @@ -429,7 +429,7 @@ export type ScrollSelectionIntoViewFunction = ( ) => void /** @internal */ -export type PortableTextMemberTypes = { +export type PortableTextMemberSchemaTypes = { annotations: ObjectSchemaType[] block: ObjectSchemaType blockObjects: ObjectSchemaType[] diff --git a/packages/@sanity/portable-text-editor/src/utils/__tests__/operationToPatches.test.ts b/packages/@sanity/portable-text-editor/src/utils/__tests__/operationToPatches.test.ts index 4d8de89823f..f70679d7d27 100644 --- a/packages/@sanity/portable-text-editor/src/utils/__tests__/operationToPatches.test.ts +++ b/packages/@sanity/portable-text-editor/src/utils/__tests__/operationToPatches.test.ts @@ -1,16 +1,16 @@ import {createEditor, Descendant} from 'slate' import {PortableTextTextBlock} from '@sanity/types' -import {getPortableTextMemberTypes} from '../getPortableTextMemberTypes' -import {type} from '../../editor/__tests__/PortableTextEditorTester' +import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes' +import {schemaType} from '../../editor/__tests__/PortableTextEditorTester' import {createOperationToPatches} from '../operationToPatches' import {withPlugins} from '../../editor/plugins' import {PortableTextEditor, PortableTextEditorProps} from '../..' -const portableTextFeatures = getPortableTextMemberTypes(type) +const portableTextFeatures = getPortableTextMemberSchemaTypes(schemaType) const operationToPatches = createOperationToPatches(portableTextFeatures) const editor = withPlugins(createEditor(), { - portableTextEditor: new PortableTextEditor({type} as PortableTextEditorProps), + portableTextEditor: new PortableTextEditor({schemaType} as PortableTextEditorProps), }) const createDefaultValue = () => diff --git a/packages/@sanity/portable-text-editor/src/utils/__tests__/patchToOperations.test.ts b/packages/@sanity/portable-text-editor/src/utils/__tests__/patchToOperations.test.ts index 5506018edb4..4abb2440de7 100644 --- a/packages/@sanity/portable-text-editor/src/utils/__tests__/patchToOperations.test.ts +++ b/packages/@sanity/portable-text-editor/src/utils/__tests__/patchToOperations.test.ts @@ -1,16 +1,16 @@ import {createEditor, Descendant} from 'slate' -import {type} from '../../editor/__tests__/PortableTextEditorTester' +import {schemaType} from '../../editor/__tests__/PortableTextEditorTester' import {createPatchToOperations} from '../patchToOperations' import {withPlugins} from '../../editor/plugins' import {keyGenerator, Patch, PortableTextEditor, PortableTextEditorProps} from '../..' import {fromSlateValue} from '../values' -import {getPortableTextMemberTypes} from '../getPortableTextMemberTypes' +import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes' -const types = getPortableTextMemberTypes(type) +const types = getPortableTextMemberSchemaTypes(schemaType) const patchToOperations = createPatchToOperations(types, keyGenerator) const editor = withPlugins(createEditor(), { - portableTextEditor: new PortableTextEditor({type} as PortableTextEditorProps), + portableTextEditor: new PortableTextEditor({schemaType} as PortableTextEditorProps), }) const createDefaultValue = (): Descendant[] => [ diff --git a/packages/@sanity/portable-text-editor/src/utils/__tests__/valueNormalization.test.tsx b/packages/@sanity/portable-text-editor/src/utils/__tests__/valueNormalization.test.tsx index 26758873192..c8f4cf7c979 100644 --- a/packages/@sanity/portable-text-editor/src/utils/__tests__/valueNormalization.test.tsx +++ b/packages/@sanity/portable-text-editor/src/utils/__tests__/valueNormalization.test.tsx @@ -8,7 +8,7 @@ import {render, waitFor} from '@testing-library/react' import React from 'react' import {PortableTextEditor} from '../../editor/PortableTextEditor' -import {PortableTextEditorTester, type} from '../../editor/__tests__/PortableTextEditorTester' +import {PortableTextEditorTester, schemaType} from '../../editor/__tests__/PortableTextEditorTester' describe('values: normalization', () => { it("accepts incoming value with blocks without a style or markDefs prop, but doesn't leave them without them when editing them", async () => { @@ -33,7 +33,7 @@ describe('values: normalization', () => { ) diff --git a/packages/@sanity/portable-text-editor/src/utils/__tests__/values.test.ts b/packages/@sanity/portable-text-editor/src/utils/__tests__/values.test.ts index be98668bf25..a6f341ad3af 100644 --- a/packages/@sanity/portable-text-editor/src/utils/__tests__/values.test.ts +++ b/packages/@sanity/portable-text-editor/src/utils/__tests__/values.test.ts @@ -1,17 +1,17 @@ import {fromSlateValue, toSlateValue} from '../values' -import {type} from '../../editor/__tests__/PortableTextEditorTester' -import {getPortableTextMemberTypes} from '../getPortableTextMemberTypes' +import {schemaType} from '../../editor/__tests__/PortableTextEditorTester' +import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes' -const types = getPortableTextMemberTypes(type) +const schemaTypes = getPortableTextMemberSchemaTypes(schemaType) describe('toSlateValue', () => { it('checks undefined', () => { - const result = toSlateValue(undefined, {types}) + const result = toSlateValue(undefined, {schemaTypes}) expect(result).toHaveLength(0) }) it('runs given empty array', () => { - const result = toSlateValue([], {types}) + const result = toSlateValue([], {schemaTypes}) expect(result).toHaveLength(0) }) @@ -23,7 +23,7 @@ describe('toSlateValue', () => { _key: '123', }, ], - {types} + {schemaTypes} ) expect(result).toMatchObject([ @@ -44,7 +44,7 @@ describe('toSlateValue', () => { const result = toSlateValue( [ { - _type: types.block.name, + _type: schemaTypes.block.name, _key: '123', children: [ { @@ -55,7 +55,7 @@ describe('toSlateValue', () => { ], }, ], - {types} + {schemaTypes} ) expect(result).toMatchInlineSnapshot(` Array [ @@ -80,7 +80,7 @@ describe('toSlateValue', () => { const result = toSlateValue( [ { - _type: types.block.name, + _type: schemaTypes.block.name, _key: '123', children: [ { @@ -98,7 +98,7 @@ describe('toSlateValue', () => { ], }, ], - {types} + {schemaTypes} ) expect(result).toMatchInlineSnapshot(` Array [ @@ -241,8 +241,8 @@ describe('fromSlateValue', () => { style: 'normal', }, ] - const toSlate1 = toSlateValue(value, {types}, keyMap) - const toSlate2 = toSlateValue(value, {types}, keyMap) + const toSlate1 = toSlateValue(value, {schemaTypes}, keyMap) + const toSlate2 = toSlateValue(value, {schemaTypes}, keyMap) expect(toSlate1[0]).toBe(toSlate2[0]) expect(toSlate1[1]).toBe(toSlate2[1]) const fromSlate1 = fromSlateValue(toSlate1, 'block', keyMap) diff --git a/packages/@sanity/portable-text-editor/src/utils/getPortableTextMemberTypes.ts b/packages/@sanity/portable-text-editor/src/utils/getPortableTextMemberSchemaTypes.ts similarity index 95% rename from packages/@sanity/portable-text-editor/src/utils/getPortableTextMemberTypes.ts rename to packages/@sanity/portable-text-editor/src/utils/getPortableTextMemberSchemaTypes.ts index ba8f403cdc7..a431262b320 100644 --- a/packages/@sanity/portable-text-editor/src/utils/getPortableTextMemberTypes.ts +++ b/packages/@sanity/portable-text-editor/src/utils/getPortableTextMemberSchemaTypes.ts @@ -6,11 +6,11 @@ import { SchemaType, SpanSchemaType, } from '@sanity/types' -import {PortableTextMemberTypes} from '../types/editor' +import {PortableTextMemberSchemaTypes} from '../types/editor' -export function getPortableTextMemberTypes( +export function getPortableTextMemberSchemaTypes( portableTextType: ArraySchemaType -): PortableTextMemberTypes { +): PortableTextMemberSchemaTypes { if (!portableTextType) { throw new Error("Parameter 'portabletextType' missing (required)") } diff --git a/packages/@sanity/portable-text-editor/src/utils/operationToPatches.ts b/packages/@sanity/portable-text-editor/src/utils/operationToPatches.ts index e4e5fec6da6..5608366cf8f 100644 --- a/packages/@sanity/portable-text-editor/src/utils/operationToPatches.ts +++ b/packages/@sanity/portable-text-editor/src/utils/operationToPatches.ts @@ -15,13 +15,13 @@ import { import {set, insert, unset, diffMatchPatch, setIfMissing} from '../patch/PatchEvent' import type {Patch, InsertPosition} from '../types/patch' import {PatchFunctions} from '../editor/plugins/createWithPatches' -import {PortableTextMemberTypes} from '../types/editor' +import {PortableTextMemberSchemaTypes} from '../types/editor' import {fromSlateValue} from './values' import {debugWithName} from './debug' const debug = debugWithName('operationToPatches') -export function createOperationToPatches(types: PortableTextMemberTypes): PatchFunctions { +export function createOperationToPatches(types: PortableTextMemberSchemaTypes): PatchFunctions { const textBlockName = types.block.name function insertTextPatch( editor: Editor, diff --git a/packages/@sanity/portable-text-editor/src/utils/patchToOperations.ts b/packages/@sanity/portable-text-editor/src/utils/patchToOperations.ts index e401fd1c856..c6951ec8af4 100644 --- a/packages/@sanity/portable-text-editor/src/utils/patchToOperations.ts +++ b/packages/@sanity/portable-text-editor/src/utils/patchToOperations.ts @@ -5,7 +5,7 @@ import {Path, KeyedSegment, PathSegment, PortableTextBlock, PortableTextChild} f import {isEqual} from 'lodash' import type {Patch, InsertPatch, UnsetPatch, SetPatch, DiffMatchPatch} from '../types/patch' import {applyAll} from '../patch/applyPatch' -import {PortableTextMemberTypes} from '../types/editor' +import {PortableTextMemberSchemaTypes} from '../types/editor' import {toSlateValue} from './values' import {debugWithName} from './debug' import {KEY_TO_SLATE_ELEMENT} from './weakMaps' @@ -16,7 +16,7 @@ const debug = debugWithName('operationToPatches') const dmp = new DMP.diff_match_patch() export function createPatchToOperations( - types: PortableTextMemberTypes, + schemaTypes: PortableTextMemberSchemaTypes, keyGenerator: () => string ): ( editor: Editor, @@ -86,7 +86,7 @@ export function createPatchToOperations( const {items, position} = patch const blocksToInsert = toSlateValue( items as PortableTextBlock[], - {types}, + {schemaTypes}, KEY_TO_SLATE_ELEMENT.get(editor) ) as Descendant[] const posKey = findLastKey(patch.path) @@ -120,7 +120,7 @@ export function createPatchToOperations( block && toSlateValue( [{...block, children: items as PortableTextChild[]}], - {types}, + {schemaTypes}, KEY_TO_SLATE_ELEMENT.get(editor) ) diff --git a/packages/@sanity/portable-text-editor/src/utils/paths.ts b/packages/@sanity/portable-text-editor/src/utils/paths.ts index a05d8e93acc..4ddb321965b 100644 --- a/packages/@sanity/portable-text-editor/src/utils/paths.ts +++ b/packages/@sanity/portable-text-editor/src/utils/paths.ts @@ -1,12 +1,12 @@ import {isEqual} from 'lodash' import {Editor, Point, Path as SlatePath, Element} from 'slate' import {isKeySegment, Path, PortableTextBlock} from '@sanity/types' -import {EditorSelectionPoint, PortableTextMemberTypes} from '../types/editor' +import {EditorSelectionPoint, PortableTextMemberSchemaTypes} from '../types/editor' export function createKeyedPath( point: Point, value: PortableTextBlock[] | undefined, - types: PortableTextMemberTypes + types: PortableTextMemberSchemaTypes ): Path | null { const blockPath = [point.path[0]] if (!value) { diff --git a/packages/@sanity/portable-text-editor/src/utils/ranges.ts b/packages/@sanity/portable-text-editor/src/utils/ranges.ts index b2d65b35d22..475d5c74b39 100644 --- a/packages/@sanity/portable-text-editor/src/utils/ranges.ts +++ b/packages/@sanity/portable-text-editor/src/utils/ranges.ts @@ -1,12 +1,12 @@ import {PortableTextBlock} from '@sanity/types' import {BaseRange, Editor, Range} from 'slate' -import {EditorSelection, EditorSelectionPoint, PortableTextMemberTypes} from '../types/editor' +import {EditorSelection, EditorSelectionPoint, PortableTextMemberSchemaTypes} from '../types/editor' import {createArrayedPath, createKeyedPath} from './paths' export function toPortableTextRange( value: PortableTextBlock[] | undefined, range: BaseRange | Partial | null, - types: PortableTextMemberTypes + types: PortableTextMemberSchemaTypes ): EditorSelection { if (!range) { return null diff --git a/packages/@sanity/portable-text-editor/src/utils/validateValue.ts b/packages/@sanity/portable-text-editor/src/utils/validateValue.ts index 83ab8df5ea8..3ff6a571eb5 100644 --- a/packages/@sanity/portable-text-editor/src/utils/validateValue.ts +++ b/packages/@sanity/portable-text-editor/src/utils/validateValue.ts @@ -1,11 +1,11 @@ import {PortableTextBlock, PortableTextSpan, PortableTextTextBlock} from '@sanity/types' import {flatten, isObject, uniq} from 'lodash' import {set, unset, insert} from '../patch/PatchEvent' -import {InvalidValueResolution, PortableTextMemberTypes} from '../types/editor' +import {InvalidValueResolution, PortableTextMemberSchemaTypes} from '../types/editor' export function validateValue( value: PortableTextBlock[] | undefined, - types: PortableTextMemberTypes, + types: PortableTextMemberSchemaTypes, keyGenerator: () => string ): {valid: boolean; resolution: InvalidValueResolution | null} { let resolution: InvalidValueResolution | null = null diff --git a/packages/@sanity/portable-text-editor/src/utils/values.ts b/packages/@sanity/portable-text-editor/src/utils/values.ts index 42796daebec..cc29962e345 100644 --- a/packages/@sanity/portable-text-editor/src/utils/values.ts +++ b/packages/@sanity/portable-text-editor/src/utils/values.ts @@ -7,7 +7,7 @@ import { PortableTextObject, PortableTextTextBlock, } from '@sanity/types' -import {PortableTextMemberTypes} from '../types/editor' +import {PortableTextMemberSchemaTypes} from '../types/editor' const EMPTY_MARKDEFS: PortableTextObject[] = [] @@ -29,14 +29,14 @@ function keepObjectEquality( export function toSlateValue( value: PortableTextBlock[] | undefined, - {types}: {types: PortableTextMemberTypes}, + {schemaTypes}: {schemaTypes: PortableTextMemberSchemaTypes}, keyMap: Record = {} ): Descendant[] { if (value && Array.isArray(value)) { return value.map((block) => { const {_type, _key, ...rest} = block const voidChildren = [{_key: `${_key}-void-child`, _type: 'span', text: '', marks: []}] - const isPortableText = block && block._type === types.block.name + const isPortableText = block && block._type === schemaTypes.block.name if (isPortableText) { const textBlock = block as PortableTextTextBlock let hasInlines = false @@ -65,7 +65,7 @@ export function toSlateValue( return block } if (hasMissingStyle) { - rest.style = types.styles[0].value + rest.style = schemaTypes.styles[0].value } if (hasMissingMarkDefs) { rest.markDefs = EMPTY_MARKDEFS @@ -123,7 +123,7 @@ export function fromSlateValue( export function isEqualToEmptyEditor( children: Descendant[] | PortableTextBlock[], - types: PortableTextMemberTypes + types: PortableTextMemberSchemaTypes ): boolean { return ( children === undefined || diff --git a/packages/@sanity/portable-text-editor/test/web-server/components/Editor.tsx b/packages/@sanity/portable-text-editor/test/web-server/components/Editor.tsx index 8925cacd60a..9e7b3f9e8f6 100644 --- a/packages/@sanity/portable-text-editor/test/web-server/components/Editor.tsx +++ b/packages/@sanity/portable-text-editor/test/web-server/components/Editor.tsx @@ -75,7 +75,7 @@ export const Editor = ({ const renderBlock: RenderBlockFunction = useCallback((props) => { const {value: block, type, children} = props if (editor.current) { - const textType = editor.current.types.block + const textType = editor.current.schemaTypes.block // Text blocks if (type.name === textType.name) { return ( @@ -97,11 +97,11 @@ export const Editor = ({ }, []) const renderChild: RenderChildFunction = useCallback((props) => { - const {type, children} = props + const {schemaType, children} = props if (editor.current) { - const textType = editor.current.types.span + const textType = editor.current.schemaTypes.span // Text spans - if (type.name === textType.name) { + if (schemaType.name === textType.name) { return children } // Inline objects @@ -199,7 +199,7 @@ export const Editor = ({ return (