/** * Convert an article in a list item node. * @param {SummaryArticle} article * @return {Block} item */ function articleToBlock(article) { const { title, ref, articles } = article; const nodes = [ Block.create({ type: BLOCKS.TEXT, nodes: [ Inline.create({ type: INLINES.LINK, nodes: [ Text.createFromString(title) ], data: { href: ref || '' } }) ] }) ]; if (articles.size > 0) { nodes.push(articlesToBlock(articles)); } return Block.create({ type: BLOCKS.LIST_ITEM, nodes }); }
function renderPlaceholder(props) { var editor = props.editor, node = props.node; if (!editor.props.placeholder) return; if (editor.state.isComposing) return; if (node.kind != 'block') return; if (!_slate.Text.isTextList(node.nodes)) return; if (node.text != '') return; if (editor.value.document.getBlocks().size > 1) return; var style = { pointerEvents: 'none', display: 'inline-block', width: '0', maxWidth: '100%', whiteSpace: 'nowrap', opacity: '0.333' }; return _react2.default.createElement( 'span', { contentEditable: false, style: style }, editor.props.placeholder ); }
/** * Convert a summary to document. * @param {Summary} summary * @return {Document} document */ function summaryToDocument(summary) { const { parts } = summary; const nodes = [ Block.create({ type: BLOCKS.HEADING_1, nodes: [ Text.createFromString('Summary') ] }) ]; parts.forEach((part, i) => { const { title, articles } = part; if (title) { nodes.push(Block.create({ type: BLOCKS.HEADING_2, nodes: [ Text.createFromString(title) ] })); } else if (i > 0) { nodes.push(Block.create({ type: BLOCKS.HR })); } if (!articles.isEmpty()) { nodes.push(articlesToBlock(articles)); } }); return Document.create({ nodes }); }
children.forEach(child => { // If the child is a non-text node, push the current node and the new child // onto the array, then creating a new node for future selection tracking. if (Node.isNode(child) && !Text.isText(child)) { if (node.text.length || node.__anchor != null || node.__focus != null) array.push(node) array.push(child) node = Text.create() length = 0 } // If the child is a string insert it into the node. if (typeof child == 'string') { setNode(node.insertText(node.text.length, child, options.marks)) length += child.length } // If the node is a `Text` add its text and marks to the existing node. If // the existing node is empty, and the `key` option wasn't set, preserve the // child's key when updating the node. if (Text.isText(child)) { const { __anchor, __focus } = child let i = node.text.length if (!options.key && node.text.length == 0) { setNode(node.set('key', child.key)) } child.getLeaves().forEach(leaf => { let { marks } = leaf if (options.marks) marks = marks.union(options.marks) setNode(node.insertText(i, leaf.text, marks)) i += leaf.text.length }) if (__anchor != null) node.__anchor = __anchor + length if (__focus != null) node.__focus = __focus + length length += child.text.length } // If the child is a selection object store the current position. if (child == ANCHOR || child == CURSOR) node.__anchor = length if (child == FOCUS || child == CURSOR) node.__focus = length })
return () => editor.insertNodeByKey( node.key, 0, Block.create({ type: 'paragraph', nodes: [Text.create()], }) )
export function createText(tagName, attributes, children) { const { key } = attributes const leaves = createLeaves('leaves', {}, children) const text = Text.create({ key, leaves }) let length = 0 leaves.forEach(leaf => { incrementPoint(leaf, length) preservePoint(leaf, () => text) length += leaf.text.length }) return text }
parts.forEach((part, i) => { const { title, articles } = part; if (title) { nodes.push(Block.create({ type: BLOCKS.HEADING_2, nodes: [ Text.createFromString(title) ] })); } else if (i > 0) { nodes.push(Block.create({ type: BLOCKS.HR })); } if (!articles.isEmpty()) { nodes.push(articlesToBlock(articles)); } });
const push = node => { const last = nodes.last() const isString = typeof node === 'string' if (last && last.__string && (isString || node.__string)) { const text = isString ? node : node.text const { length } = last.text const next = preservePoints(last, l => l.insertText(length, text)) incrementPoints(node, length) copyPoints(node, next) next.__string = true nodes = nodes.pop().push(next) } else if (isString) { node = Text.create({ text: node }) node.__string = true nodes = nodes.push(node) } else { nodes = nodes.push(node) } }
/** * Exit the current table, by inserting a default block after the table. */ function onModEnter(event, change, opts) { var state = change.state; if (!state.isCollapsed) { return; } event.preventDefault(); var exitBlock = Slate.Block.create({ type: opts.exitBlockType, nodes: [Slate.Text.create('')] }); var cell = state.startBlock; var table = TablePosition.create(state, cell).table; var tableParent = state.document.getParent(table.key); var insertionIndex = tableParent.nodes.indexOf(table) + 1; return change.insertNodeByKey(tableParent.key, insertionIndex, exitBlock).collapseToStartOf(exitBlock); }
export function createText(tagName, attributes, children) { const { key, marks } = attributes const list = createChildren(children) let node if (list.size > 1) { throw new Error( `The <text> hyperscript tag must only contain a single node's worth of children.` ) } else if (list.size === 0) { node = Text.create({ key }) } else { node = list.first() node = preservePoints(node, n => { if (key) n = n.set('key', key) if (marks) n = n.set('marks', Mark.createSet(marks)) return n }) } return node }
function renderPlaceholder(props) { const { editor, node } = props if (!editor.props.placeholder) return if (editor.state.isComposing) return if (node.object != 'block') return if (!Text.isTextList(node.nodes)) return if (node.text != '') return if (editor.value.document.getBlocks().size > 1) return const style = { pointerEvents: 'none', display: 'inline-block', width: '0', maxWidth: '100%', whiteSpace: 'nowrap', opacity: '0.333', } return ( <span contentEditable={false} style={style}> {editor.props.placeholder} </span> ) }
texts: create('List<Text>', function (v) { return slate.Text.isTextList(v); })
text: create('Text', function (v) { return slate.Text.isText(v); }),
/** @jsx h */ import h from '../..' import { Value, Document, Block, Text } from 'slate' export const input = ( <value normalize={false}> <document> <block type="paragraph">Valid block</block> <text>Invalid text</text> </document> </value> ) export const output = Value.fromJSON( { document: Document.create({ nodes: [ Block.create({ type: 'paragraph', nodes: [Text.create('Valid block')], }), Text.create('Invalid text'), ], }), }, { normalize: false } )
texts: create('List<Text>', v => Text.isTextList(v)),
text: create('Text', v => Text.isText(v)),
function createChildren(children, options = {}) { const array = [] let length = 0 // When creating the new node, try to preserve a key if one exists. const firstText = children.find(c => Text.isText(c)) const key = options.key ? options.key : firstText ? firstText.key : undefined let node = Text.create({ key }) // Create a helper to update the current node while preserving any stored // anchor or focus information. function setNode(next) { const { __anchor, __focus } = node if (__anchor != null) next.__anchor = __anchor if (__focus != null) next.__focus = __focus node = next } children.forEach(child => { // If the child is a non-text node, push the current node and the new child // onto the array, then creating a new node for future selection tracking. if (Node.isNode(child) && !Text.isText(child)) { if (node.text.length || node.__anchor != null || node.__focus != null) array.push(node) array.push(child) node = Text.create() length = 0 } // If the child is a string insert it into the node. if (typeof child == 'string') { setNode(node.insertText(node.text.length, child, options.marks)) length += child.length } // If the node is a `Text` add its text and marks to the existing node. If // the existing node is empty, and the `key` option wasn't set, preserve the // child's key when updating the node. if (Text.isText(child)) { const { __anchor, __focus } = child let i = node.text.length if (!options.key && node.text.length == 0) { setNode(node.set('key', child.key)) } child.getLeaves().forEach(leaf => { let { marks } = leaf if (options.marks) marks = marks.union(options.marks) setNode(node.insertText(i, leaf.text, marks)) i += leaf.text.length }) if (__anchor != null) node.__anchor = __anchor + length if (__focus != null) node.__focus = __focus + length length += child.text.length } // If the child is a selection object store the current position. if (child == ANCHOR || child == CURSOR) node.__anchor = length if (child == FOCUS || child == CURSOR) node.__focus = length }) // Make sure the most recent node is added. array.push(node) return array }
const firstText = children.find(c => Text.isText(c))