@blocksuite/store
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -63,3 +63,5 @@ import * as Y from 'yjs'; | ||
getPreviousSibling(block: BaseBlockModel): BaseBlockModel | null; | ||
getPreviousSiblings(block: BaseBlockModel): BaseBlockModel[]; | ||
getNextSibling(block: BaseBlockModel): BaseBlockModel | null; | ||
getNextSiblings(block: BaseBlockModel): BaseBlockModel[]; | ||
addBlock<T extends BlockProps>(blockProps: Partial<T>, parent?: BaseBlockModel | string, parentIndex?: number): string; | ||
@@ -66,0 +68,0 @@ updateBlockById(id: string, props: Partial<BlockProps>): void; |
@@ -123,13 +123,44 @@ import * as Y from 'yjs'; | ||
const parent = this.getParent(block); | ||
const index = parent?.children.indexOf(block) ?? -1; | ||
return parent?.children[index - 1] ?? null; | ||
if (!parent) { | ||
return null; | ||
} | ||
const index = parent.children.indexOf(block); | ||
if (index === -1) { | ||
throw new Error("Failed to getPreviousSiblings! Block not found in parent's children"); | ||
} | ||
return parent.children[index - 1] ?? null; | ||
} | ||
getPreviousSiblings(block) { | ||
const parent = this.getParent(block); | ||
if (!parent) { | ||
return []; | ||
} | ||
const index = parent.children.indexOf(block); | ||
if (index === -1) { | ||
throw new Error("Failed to getPreviousSiblings! Block not found in parent's children"); | ||
} | ||
return parent.children.slice(0, index); | ||
} | ||
getNextSibling(block) { | ||
const parent = this.getParent(block); | ||
const index = parent?.children.indexOf(block) ?? -1; | ||
if (index === -1) { | ||
if (!parent) { | ||
return null; | ||
} | ||
return parent?.children[index + 1] ?? null; | ||
const index = parent.children.indexOf(block); | ||
if (index === -1) { | ||
throw new Error("Failed to getPreviousSiblings! Block not found in parent's children"); | ||
} | ||
return parent.children[index + 1] ?? null; | ||
} | ||
getNextSiblings(block) { | ||
const parent = this.getParent(block); | ||
if (!parent) { | ||
return []; | ||
} | ||
const index = parent.children.indexOf(block); | ||
if (index === -1) { | ||
throw new Error("Failed to getNextSiblings! Block not found in parent's children"); | ||
} | ||
return parent.children.slice(index + 1); | ||
} | ||
addBlock(blockProps, parent, parentIndex) { | ||
@@ -182,2 +213,10 @@ if (!blockProps.flavour) { | ||
} | ||
// TODO diff children changes | ||
// All child nodes will be deleted in the current behavior, then added again. | ||
// Through diff children changes, the experience can be improved. | ||
if (props.children) { | ||
const yChildren = new Y.Array(); | ||
yChildren.insert(0, props.children.map(child => child.id)); | ||
yBlock.set('sys:children', yChildren); | ||
} | ||
syncBlockProps(yBlock, props, this._ignoredKeys); | ||
@@ -357,2 +396,4 @@ }); | ||
const props = {}; | ||
let hasPropsUpdate = false; | ||
let hasChildrenUpdate = false; | ||
for (const key of event.keysChanged) { | ||
@@ -362,6 +403,23 @@ // TODO use schema | ||
continue; | ||
// Update children | ||
if (key === 'sys:children') { | ||
hasChildrenUpdate = true; | ||
const yChildren = event.target.get('sys:children'); | ||
if (!(yChildren instanceof Y.Array)) { | ||
console.error('Failed to update block children!, sys:children is not an Y array', event, yChildren); | ||
continue; | ||
} | ||
model.childMap = createChildMap(yChildren); | ||
model.children = yChildren.map(id => this._blockMap.get(id)); | ||
continue; | ||
} | ||
// Update props | ||
hasPropsUpdate = true; | ||
props[key.replace('prop:', '')] = event.target.get(key); | ||
} | ||
Object.assign(model, props); | ||
model.propsUpdated.emit(); | ||
if (hasPropsUpdate) { | ||
Object.assign(model, props); | ||
model.propsUpdated.emit(); | ||
} | ||
hasChildrenUpdate && model.childrenUpdated.emit(); | ||
} | ||
@@ -368,0 +426,0 @@ _handleYEvent(event) { |
{ | ||
"name": "@blocksuite/store", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "BlockSuite data store built for general purpose state management.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -14,2 +14,9 @@ import type { PlaywrightTestConfig } from '@playwright/test'; | ||
if (process.env.CI) { | ||
config.webServer = { | ||
command: 'pnpm dev', | ||
port: 5173, | ||
}; | ||
} | ||
export default config; |
@@ -170,15 +170,56 @@ import * as Y from 'yjs'; | ||
const parent = this.getParent(block); | ||
const index = parent?.children.indexOf(block) ?? -1; | ||
return parent?.children[index - 1] ?? null; | ||
if (!parent) { | ||
return null; | ||
} | ||
const index = parent.children.indexOf(block); | ||
if (index === -1) { | ||
throw new Error( | ||
"Failed to getPreviousSiblings! Block not found in parent's children" | ||
); | ||
} | ||
return parent.children[index - 1] ?? null; | ||
} | ||
getPreviousSiblings(block: BaseBlockModel) { | ||
const parent = this.getParent(block); | ||
if (!parent) { | ||
return []; | ||
} | ||
const index = parent.children.indexOf(block); | ||
if (index === -1) { | ||
throw new Error( | ||
"Failed to getPreviousSiblings! Block not found in parent's children" | ||
); | ||
} | ||
return parent.children.slice(0, index); | ||
} | ||
getNextSibling(block: BaseBlockModel) { | ||
const parent = this.getParent(block); | ||
const index = parent?.children.indexOf(block) ?? -1; | ||
if (index === -1) { | ||
if (!parent) { | ||
return null; | ||
} | ||
return parent?.children[index + 1] ?? null; | ||
const index = parent.children.indexOf(block); | ||
if (index === -1) { | ||
throw new Error( | ||
"Failed to getPreviousSiblings! Block not found in parent's children" | ||
); | ||
} | ||
return parent.children[index + 1] ?? null; | ||
} | ||
getNextSiblings(block: BaseBlockModel) { | ||
const parent = this.getParent(block); | ||
if (!parent) { | ||
return []; | ||
} | ||
const index = parent.children.indexOf(block); | ||
if (index === -1) { | ||
throw new Error( | ||
"Failed to getNextSiblings! Block not found in parent's children" | ||
); | ||
} | ||
return parent.children.slice(index + 1); | ||
} | ||
addBlock<T extends BlockProps>( | ||
@@ -246,2 +287,14 @@ blockProps: Partial<T>, | ||
// TODO diff children changes | ||
// All child nodes will be deleted in the current behavior, then added again. | ||
// Through diff children changes, the experience can be improved. | ||
if (props.children) { | ||
const yChildren = new Y.Array<string>(); | ||
yChildren.insert( | ||
0, | ||
props.children.map(child => child.id) | ||
); | ||
yBlock.set('sys:children', yChildren); | ||
} | ||
syncBlockProps(yBlock, props, this._ignoredKeys); | ||
@@ -479,9 +532,35 @@ }); | ||
const props: Partial<BlockProps> = {}; | ||
let hasPropsUpdate = false; | ||
let hasChildrenUpdate = false; | ||
for (const key of event.keysChanged) { | ||
// TODO use schema | ||
if (key === 'prop:text') continue; | ||
// Update children | ||
if (key === 'sys:children') { | ||
hasChildrenUpdate = true; | ||
const yChildren = event.target.get('sys:children'); | ||
if (!(yChildren instanceof Y.Array)) { | ||
console.error( | ||
'Failed to update block children!, sys:children is not an Y array', | ||
event, | ||
yChildren | ||
); | ||
continue; | ||
} | ||
model.childMap = createChildMap(yChildren); | ||
model.children = yChildren.map( | ||
id => this._blockMap.get(id) as BaseBlockModel | ||
); | ||
continue; | ||
} | ||
// Update props | ||
hasPropsUpdate = true; | ||
props[key.replace('prop:', '')] = event.target.get(key); | ||
} | ||
Object.assign(model, props); | ||
model.propsUpdated.emit(); | ||
if (hasPropsUpdate) { | ||
Object.assign(model, props); | ||
model.propsUpdated.emit(); | ||
} | ||
hasChildrenUpdate && model.childrenUpdated.emit(); | ||
} | ||
@@ -488,0 +567,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
428753
7203