slate#Ancestor TypeScript Examples
The following examples show how to use
slate#Ancestor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: isListActive.ts From payload with MIT License | 6 votes |
isListActive = (editor: Editor, format: string): boolean => {
let parentLI: NodeEntry<Ancestor>;
try {
parentLI = Editor.parent(editor, editor.selection);
} catch (e) {
// swallow error, Slate
}
if (parentLI?.[1]?.length > 0) {
const ancestor = Editor.above(editor, {
at: parentLI[1],
});
return Element.isElement(ancestor[0]) && ancestor[0].type === format;
}
return false;
}
Example #2
Source File: op-generator.ts From slate-ot with MIT License | 6 votes |
generateRandomInsertNodeOp = (snapshot): Operation => {
const randomPath = getRandomPathTo(snapshot);
const parent = <Ancestor>Node.get(snapshot, Path.parent(randomPath));
let node;
if (parent.children[0] && Text.isText(parent.children[0])) {
node = { text: fuzzer.randomWord() };
} else if (!parent.children[0] && fuzzer.randomInt(3) === 0) {
node = { text: fuzzer.randomWord() };
} else if (fuzzer.randomInt(2) === 0) {
node = {
type: BLOCKS[fuzzer.randomInt(BLOCKS.length)],
children: [{ text: fuzzer.randomWord() }, { text: fuzzer.randomWord() }],
};
} else {
node = {
type: BLOCKS[fuzzer.randomInt(BLOCKS.length)],
children: [
{
type: BLOCKS[fuzzer.randomInt(BLOCKS.length)],
children: [
{ text: fuzzer.randomWord() },
{ text: fuzzer.randomWord() },
],
},
],
};
}
return {
type: 'insert_node',
path: randomPath,
node,
};
}
Example #3
Source File: op-generator.ts From slate-ot with MIT License | 6 votes |
generateRandomMoveNodeOp = (snapshot): Operation | null => {
let count = 0;
while (count < 10) {
count++;
const path = getRandomPathFrom(snapshot);
const newPath = getRandomPathTo(snapshot);
if (Path.isSibling(path, newPath)) {
const parent = <Ancestor>Node.get(snapshot, Path.parent(newPath));
if (newPath[newPath.length - 1] == parent.children.length) {
newPath[newPath.length - 1]--;
}
}
if (!Path.isAncestor(path, newPath)) {
return {
type: 'move_node',
path,
newPath,
};
}
}
return null;
}
Example #4
Source File: weak-maps.ts From slate-vue with MIT License | 5 votes |
NODE_TO_PARENT: WeakMap<Node, Ancestor> = new WeakMap()
Example #5
Source File: weak-maps.ts From slate-angular with MIT License | 5 votes |
NODE_TO_PARENT: WeakMap<Node, Ancestor> = new WeakMap()
Example #6
Source File: children.tsx From slate-vue with MIT License | 4 votes |
Children: any = tsx.component({
props: {
// only element or editor
node: {
type: Object as PropType<Ancestor>
}
},
components: {
TextComponent,
ElementComponent,
fragment
},
mixins: [SlateMixin],
mounted() {
elementWatcherPlugin(this, 'children')
},
render() {
const editor = this.$editor;
const {node} = this;
const path = VueEditor.findPath(editor, node)
const isLeafBlock =
Element.isElement(node) &&
!editor.isInline(node) &&
Editor.hasInlines(editor, node)
const children = []
const childArr: any = Editor.isEditor(node) ? (node as VueEditor)._state : (node as Element).children
// cacheVnode in manual to reuse
let cacheVnode = null;
for(let i=0;i<childArr.length;i++) {
const n = childArr[i] as Descendant;
const key = VueEditor.findKey(editor, n)
const p = path.concat(i);
const range = Editor.range(editor, p)
// set n and its index in children
NODE_TO_INDEX.set(n, i)
// set n and its parent
NODE_TO_PARENT.set(n, node)
// when modify vnode, only new vnode or spliting vnode must be update, others will be reuse
// #62, #63: sometimes(like paste) no cacheVnode but have key, avoid getting in
if(editor._operation && KEY_TO_VNODE.get(key)) {
const operationPath = (editor._operation as any).path as Path
// split_node
if(editor._operation.type === 'split_node') {
// only sibling
if(Path.isSibling(p, operationPath)) {
if(!Path.equals(p, operationPath) && !Path.equals(p, Path.next(operationPath))) {
cacheVnode = KEY_TO_VNODE.get(key)
children.push(cacheVnode)
continue;
}
}
}
// merge_node
if(editor._operation.type === 'merge_node') {
const parentPath = Path.parent(operationPath)
if(Path.isSibling(p, parentPath)) {
if(!Path.isParent(p, operationPath)) {
cacheVnode = KEY_TO_VNODE.get(key)
children.push(cacheVnode)
continue;
}
}
}
// remove_node
if(editor._operation.type === 'remove_node') {
if(Path.isSibling(p, operationPath)) {
if(!Path.equals(p, operationPath)) {
cacheVnode = KEY_TO_VNODE.get(key)
children.push(cacheVnode)
continue;
}
}
}
}
if(Element.isElement(n)) {
// set selected
cacheVnode =
<ElementComponent
element={n}
key={key.id}
/>
children.push(cacheVnode)
} else {
cacheVnode = <TextComponent
isLast={isLeafBlock && i === childArr.length - 1}
parent={node}
text={n}
key={key.id}
/>
children.push(cacheVnode)
}
// set key and vnode
KEY_TO_VNODE.set(key, cacheVnode as any)
}
return <fragment>{children}</fragment>;
}
})