export function createSelection(tagName, attributes, children) { const anchor = children.find(c => c instanceof AnchorPoint) const focus = children.find(c => c instanceof FocusPoint) const { marks, focused } = attributes const selection = Selection.create({ marks, isFocused: focused, anchor: anchor && { key: anchor.key, offset: anchor.offset, path: anchor.path, }, focus: focus && { key: focus.key, offset: focus.offset, path: focus.path, }, }) return selection }
export function createValue(tagName, attributes, children) { const { data } = attributes const document = children.find(Document.isDocument) let selection = children.find(Selection.isSelection) let anchor let focus let marks let isFocused let decorations = [] const partials = {} // Search the document's texts to see if any of them have the anchor or // focus information saved, or decorations applied. if (document) { document.getTexts().forEach(text => { const { __anchor, __decorations, __focus } = text if (__anchor != null) { anchor = Point.create({ key: text.key, offset: __anchor.offset }) marks = __anchor.marks isFocused = __anchor.isFocused } if (__focus != null) { focus = Point.create({ key: text.key, offset: __focus.offset }) marks = __focus.marks isFocused = __focus.isFocused } if (__decorations != null) { for (const dec of __decorations) { const { id } = dec const partial = partials[id] delete partials[id] if (!partial) { dec.key = text.key partials[id] = dec continue } const decoration = Decoration.create({ anchor: { key: partial.key, offset: partial.offset, }, focus: { key: text.key, offset: dec.offset, }, mark: { type: dec.type, data: dec.data, }, }) decorations.push(decoration) } } }) } if (Object.keys(partials).length > 0) { throw new Error( `Slate hyperscript must have both a start and an end defined for each decoration using the \`key=\` prop.` ) } if (anchor && !focus) { throw new Error( `Slate hyperscript ranges must have both \`<anchor />\` and \`<focus />\` defined if one is defined, but you only defined \`<anchor />\`. For collapsed selections, use \`<cursor />\` instead.` ) } if (!anchor && focus) { throw new Error( `Slate hyperscript ranges must have both \`<anchor />\` and \`<focus />\` defined if one is defined, but you only defined \`<focus />\`. For collapsed selections, use \`<cursor />\` instead.` ) } if (anchor || focus) { if (!selection) { selection = Selection.create({ anchor, focus, isFocused, marks }) } else { selection = selection.setPoints([anchor, focus]) } } else if (!selection) { selection = Selection.create() } selection = selection.normalize(document) if (decorations.length > 0) { decorations = decorations.map(d => d.normalize(document)) } const value = Value.fromJSON({ data, decorations, document, selection, ...attributes, }) return value }
export function createValue(tagName, attributes, children) { const { data } = attributes const document = children.find(Document.isDocument) let selection = children.find(Selection.isSelection) let anchor let focus let marks let isFocused let annotations = {} const partials = {} // Search the document's texts to see if any of them have the anchor or // focus information saved, or annotations applied. if (document) { for (const [node, path] of document.texts()) { const { __anchor, __annotations, __focus } = node if (__anchor != null) { anchor = Point.create({ path, key: node.key, offset: __anchor.offset }) marks = __anchor.marks isFocused = __anchor.isFocused } if (__focus != null) { focus = Point.create({ path, key: node.key, offset: __focus.offset }) marks = __focus.marks isFocused = __focus.isFocused } if (__annotations != null) { for (const ann of __annotations) { const { id } = ann const partial = partials[id] delete partials[id] if (!partial) { ann.key = node.key partials[id] = ann continue } const annotation = Annotation.create({ key: id, type: ann.type, data: ann.data, anchor: { key: partial.key, path: document.getPath(partial.key), offset: partial.offset, }, focus: { path, key: node.key, offset: ann.offset, }, }) annotations[id] = annotation } } } } if (Object.keys(partials).length > 0) { throw new Error( `Slate hyperscript must have both a start and an end defined for each annotation using the \`key=\` prop.` ) } if (anchor && !focus) { throw new Error( `Slate hyperscript ranges must have both \`<anchor />\` and \`<focus />\` defined if one is defined, but you only defined \`<anchor />\`. For collapsed selections, use \`<cursor />\` instead.` ) } if (!anchor && focus) { throw new Error( `Slate hyperscript ranges must have both \`<anchor />\` and \`<focus />\` defined if one is defined, but you only defined \`<focus />\`. For collapsed selections, use \`<cursor />\` instead.` ) } if (anchor || focus) { if (!selection) { selection = Selection.create({ anchor, focus, isFocused, marks }) } else { selection = selection.setPoints([anchor, focus]) } } else if (!selection) { selection = Selection.create() } selection = selection.normalize(document) if (annotations.length > 0) { annotations = annotations.map(a => a.normalize(document)) } const value = Value.fromJSON({ data, annotations, document, selection, ...attributes, }) return value }