Slate snippets
editor.selection.anchor.path
const parentPath = Path.parent(path);
const node = Node.get(editor, path)
const path = ReactEditor.findPath(editor, node)
const domNode = ReactEditor.toDOMNode(editor, node)
const [blockNode, path] = Editor.above(editor, {match: n => editor.isBlock(n)})
const [node, path] = Editor.parent(editor, nodePath) // <- using specific node path, see above how to get paths
ReactEditor.focus(editor);
Transforms.select(editor, Editor.end(editor, []));
Transforms.insertText(editor, 'some text');
Transforms.insertNodes(editor, [
{type:'inline_type', children:[{text: 'some text', marks:[]}]},
{text: ' and some text after the inline', marks: []}
]
);
Transforms.insertNodes(editor, [
{type:'paragraph', children:[{text: 'some text', marks:[]}]},
],
{at:[0]}
);
Transforms.setNodes(editor, {type: 'paragraph'}, {at: path})
Transforms.insertText(editor, 'new text', {at: path})
Transforms.insertNodes(editor, [
{ type: 'link', url:'x', children: [{ text:'mja', marks:[] }] },
{ text: '', marks:[] },
]);
const nextPoint = Editor.after(editor, editor.selection.anchor);
Editor.setSelection(editor, {anchor:nextPoint, focus:nextPoint})
Transforms.insertText(editor, 'text in the following text node')
import * as plugins from './plugins/';
export const withPlugins = (editor) => {
for(let plugin in plugins) {
if(typeof plugins[plugin] !== 'function') continue;
const pluginEditor = plugins[plugin](editor);
if(pluginEditor !== editor) continue; // Invalid plugin
editor = pluginEditor;
}
return editor;
}
the file plugins/index.js exports all the plugins, e.g. export * from "./plugin1", export * from "./plugin2" etc
const findById = (root, id, path=[]) => {
if(!root || !root.children || !id) return;
const childLen = root.children.length;
for(let i=0;i<childLen;i++) {
const child = root.children[i];
if(child.data && child.id === id) return [child, [...path, i]];
const potential = child.children && nodeById(child, id, [...path, i]);
if(potential) return potential;
}
}
Call with editor.addData(data, node) or editor.addData(data, path)
editor.addData = (data, nodeOrPath) => {
const isNode = Node.isNode(nodeOrPath);
if(!isNode && !Path.isPath(nodeOrPath)) return;
const node = isNode ? nodeOrPath : Node.get(editor, nodeOrPath);
const path = !isNode ? nodeOrPath : ReactEditor.findPath(editor, node);
Editor.setNodes(editor, {data: deepmerge(data, node.data)}, {at: path})
}
You could use 'deepmerge' or other library for merging data
const convertNode = (node) => {
const { object, type, data, nodes, ...rest } = node
// We drop `object`, pull up data, convert `nodes` to children and copy the rest across
const element = {
type,
...rest,
...(nodes ? { children: nodes.map(convertNode) } : {}),
}
if ((!element.type || element.type === 'text') && typeof element.text !== 'undefined') {
delete element.type;
if(Array.isArray(element.marks)) {
for(const mark of element.marks) {
if(typeof mark === 'string')
element[mark] = true
else if(typeof mark === 'object' && mark.type) {
element[mark.type] = mark.hasOwnProperty('value') ? mark.value : true
}
}
}
}
if(data) element.data = data;
// if(element.type) element.type = element.type.replace("-", "_")
// Atomic blocks must now have children
if (element.type && !element.children) {
element.children = [
{
text: '',
},
]
}
return element
};
const convertSlate047to050 = (object) => {
const { nodes } = object.document
return nodes.map(convertNode)
}