@blocksuite/block-std
Advanced tools
Comparing version 0.0.0-20230829150056-df43987c-nightly to 0.0.0-20230830111255-92eab248-nightly
@@ -10,4 +10,4 @@ export type BaseSelectionOptions = { | ||
get blockId(): string; | ||
is<T extends BlockSuiteSelectionType>(type: T): this is BlockSuiteSelectionInstance[T]; | ||
get type(): BlockSuiteSelectionType; | ||
is<T extends BlockSuite.SelectionType>(type: T): this is BlockSuite.SelectionInstance[T]; | ||
get type(): BlockSuite.SelectionType; | ||
get group(): string; | ||
@@ -14,0 +14,0 @@ abstract equals(other: BaseSelection): boolean; |
@@ -22,3 +22,3 @@ import { DisposableGroup, Slot } from '@blocksuite/global/utils'; | ||
private _jsonToSelection; | ||
getInstance<T extends BlockSuiteSelectionType>(type: T, ...args: ConstructorParameters<BlockSuiteSelection[T]>): BlockSuiteSelectionInstance[T]; | ||
getInstance<T extends BlockSuite.SelectionType>(type: T, ...args: ConstructorParameters<BlockSuite.Selection[T]>): BlockSuite.SelectionInstance[T]; | ||
get value(): BaseSelection[]; | ||
@@ -30,4 +30,4 @@ set(selections: BaseSelection[]): void; | ||
clear(types?: string[]): void; | ||
find<T extends BlockSuiteSelectionType>(type: T): BlockSuiteSelectionInstance[T] | undefined; | ||
filter<T extends BlockSuiteSelectionType>(type: T): BlockSuiteSelectionInstance[T][]; | ||
find<T extends BlockSuite.SelectionType>(type: T): BlockSuite.SelectionInstance[T] | undefined; | ||
filter<T extends BlockSuite.SelectionType>(type: T): BlockSuite.SelectionInstance[T][]; | ||
get remoteSelections(): { | ||
@@ -34,0 +34,0 @@ [k: string]: BaseSelection[]; |
@@ -10,6 +10,8 @@ import { BaseSelection } from '../base.js'; | ||
declare global { | ||
interface BlockSuiteSelection { | ||
block: typeof BlockSelection; | ||
namespace BlockSuite { | ||
interface Selection { | ||
block: typeof BlockSelection; | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=block.d.ts.map |
@@ -5,7 +5,9 @@ export * from './block.js'; | ||
declare global { | ||
type BlockSuiteSelectionType = keyof BlockSuiteSelection; | ||
type BlockSuiteSelectionInstance = { | ||
[P in BlockSuiteSelectionType]: InstanceType<BlockSuiteSelection[P]>; | ||
}; | ||
namespace BlockSuite { | ||
type SelectionType = keyof Selection; | ||
type SelectionInstance = { | ||
[P in SelectionType]: InstanceType<Selection[P]>; | ||
}; | ||
} | ||
} | ||
//# sourceMappingURL=index.d.ts.map |
@@ -17,6 +17,8 @@ import { BaseSelection } from '../base.js'; | ||
declare global { | ||
interface BlockSuiteSelection { | ||
surface: typeof SurfaceSelection; | ||
namespace BlockSuite { | ||
interface Selection { | ||
surface: typeof SurfaceSelection; | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=surface.d.ts.map |
@@ -26,6 +26,8 @@ import { BaseSelection } from '../base.js'; | ||
declare global { | ||
interface BlockSuiteSelection { | ||
text: typeof TextSelection; | ||
namespace BlockSuite { | ||
interface Selection { | ||
text: typeof TextSelection; | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=text.d.ts.map |
@@ -6,7 +6,5 @@ import type { BlockStore } from './block-store.js'; | ||
view: T; | ||
type: BlockSuite.ViewType; | ||
}; | ||
export type NodeViewLeaf<T> = NodeView<T> & { | ||
type: keyof BlockSuiteView; | ||
}; | ||
export type NodeViewTree<T> = NodeViewLeaf<T> & { | ||
export type NodeViewTree<T> = NodeView<T> & { | ||
children: NodeViewTree<T>[]; | ||
@@ -16,2 +14,3 @@ }; | ||
export interface BlockSuiteViewSpec<T = any> { | ||
type: BlockSuite.ViewType; | ||
fromDOM: (node: Node) => null | NodeView<T>; | ||
@@ -26,12 +25,13 @@ toDOM: (nodeView: NodeView<T>) => Element; | ||
private _observer; | ||
readonly viewSpec: Map<string, BlockSuiteViewSpec<any>>; | ||
readonly viewSpec: Set<BlockSuiteViewSpec<any>>; | ||
constructor(blockStore: BlockStore); | ||
getChildren: (path: string[]) => NodeViewTree<NodeViewType>[]; | ||
register<T extends keyof BlockSuiteView>(type: T, spec: BlockSuiteView[T]): void; | ||
getNodeView: (node: Node) => NodeViewLeaf<NodeViewType> | null; | ||
register<T extends BlockSuite.ViewType>(spec: BlockSuite.View[T]): void; | ||
getNodeView: (node: Node) => NodeView<NodeViewType> | null; | ||
calculatePath: (node: Node) => string[]; | ||
calculateNodeViewPath: (node: Node) => NodeViewLeaf<NodeViewType>[]; | ||
private _getViewSpec; | ||
private _calculateNodeViewPath; | ||
getNodeViewTree: () => NodeViewTree<NodeViewType>; | ||
fromPath: (path: string[]) => NodeViewTree<NodeViewType> | null; | ||
viewFromPath<T extends keyof BlockSuiteView>(type: T, path: string[]): null | SpecToNodeView<BlockSuiteView[T]>; | ||
viewFromPath<T extends BlockSuite.ViewType>(type: T, path: string[]): null | SpecToNodeView<BlockSuite.View[T]>; | ||
viewFromPath<T extends BlockSuiteViewSpec>(type: string, path: string[]): null | SpecToNodeView<T>; | ||
@@ -48,3 +48,6 @@ walkThrough: (fn: (nodeView: NodeViewTree<NodeViewType>, index: number, parent: NodeViewTree<NodeViewType>) => undefined | null | true, path?: string[]) => void; | ||
declare global { | ||
interface BlockSuiteView { | ||
namespace BlockSuite { | ||
interface View { | ||
} | ||
type ViewType = keyof View; | ||
} | ||
@@ -51,0 +54,0 @@ } |
@@ -12,3 +12,3 @@ import { assertExists } from '@blocksuite/global/utils'; | ||
this._cachedPath = new Map(); | ||
this.viewSpec = new Map(); | ||
this.viewSpec = new Set(); | ||
this.getChildren = (path) => { | ||
@@ -22,7 +22,6 @@ const node = this.fromPath(path); | ||
this.getNodeView = (node) => { | ||
for (const [type, spec] of this.viewSpec.entries()) { | ||
for (const [_, spec] of this.viewSpec.entries()) { | ||
const view = spec.fromDOM(node); | ||
if (view) { | ||
return { | ||
type, | ||
...view, | ||
@@ -35,6 +34,9 @@ }; | ||
this.calculatePath = (node) => { | ||
const path = this.calculateNodeViewPath(node); | ||
const path = this._calculateNodeViewPath(node); | ||
return path.map(x => x.id); | ||
}; | ||
this.calculateNodeViewPath = (node) => { | ||
this._getViewSpec = (type) => { | ||
return Array.from(this.viewSpec).find(spec => spec.type === type); | ||
}; | ||
this._calculateNodeViewPath = (node) => { | ||
if (this._cachedPath.has(node)) { | ||
@@ -51,3 +53,3 @@ return this._cachedPath.get(node); | ||
} | ||
const spec = this.viewSpec.get(nodeView.type); | ||
const spec = this._getViewSpec(nodeView.type); | ||
assertExists(spec); | ||
@@ -73,3 +75,3 @@ const next = spec.toDOM(nodeView).parentElement; | ||
} | ||
const spec = this.viewSpec.get(nodeView.type); | ||
const spec = this._getViewSpec(nodeView.type); | ||
assertExists(spec); | ||
@@ -226,4 +228,4 @@ const children = spec | ||
} | ||
register(type, spec) { | ||
this.viewSpec.set(type, spec); | ||
register(spec) { | ||
this.viewSpec.add(spec); | ||
} | ||
@@ -230,0 +232,0 @@ viewFromPath(type, path) { |
{ | ||
"name": "@blocksuite/block-std", | ||
"version": "0.0.0-20230829150056-df43987c-nightly", | ||
"version": "0.0.0-20230830111255-92eab248-nightly", | ||
"description": "Std for blocksuite blocks", | ||
@@ -12,10 +12,10 @@ "main": "dist/index.js", | ||
"peerDependencies": { | ||
"@blocksuite/store": "0.0.0-20230829150056-df43987c-nightly" | ||
"@blocksuite/store": "0.0.0-20230830111255-92eab248-nightly" | ||
}, | ||
"dependencies": { | ||
"w3c-keyname": "^2.2.8", | ||
"@blocksuite/global": "0.0.0-20230829150056-df43987c-nightly" | ||
"@blocksuite/global": "0.0.0-20230830111255-92eab248-nightly" | ||
}, | ||
"devDependencies": { | ||
"@blocksuite/store": "0.0.0-20230829150056-df43987c-nightly" | ||
"@blocksuite/store": "0.0.0-20230830111255-92eab248-nightly" | ||
}, | ||
@@ -22,0 +22,0 @@ "exports": { |
@@ -26,11 +26,11 @@ import { PathFinder } from '../utils/index.js'; | ||
is<T extends BlockSuiteSelectionType>( | ||
is<T extends BlockSuite.SelectionType>( | ||
type: T | ||
): this is BlockSuiteSelectionInstance[T] { | ||
): this is BlockSuite.SelectionInstance[T] { | ||
return this.type === type; | ||
} | ||
get type(): BlockSuiteSelectionType { | ||
get type(): BlockSuite.SelectionType { | ||
return (this.constructor as SelectionConstructor) | ||
.type as BlockSuiteSelectionType; | ||
.type as BlockSuite.SelectionType; | ||
} | ||
@@ -37,0 +37,0 @@ |
@@ -56,6 +56,6 @@ import { DisposableGroup, Slot } from '@blocksuite/global/utils'; | ||
getInstance<T extends BlockSuiteSelectionType>( | ||
getInstance<T extends BlockSuite.SelectionType>( | ||
type: T, | ||
...args: ConstructorParameters<BlockSuiteSelection[T]> | ||
): BlockSuiteSelectionInstance[T] { | ||
...args: ConstructorParameters<BlockSuite.Selection[T]> | ||
): BlockSuite.SelectionInstance[T] { | ||
const ctor = this._selectionConstructors[type]; | ||
@@ -65,3 +65,3 @@ if (!ctor) { | ||
} | ||
return new ctor(...args) as BlockSuiteSelectionInstance[T]; | ||
return new ctor(...args) as BlockSuite.SelectionInstance[T]; | ||
} | ||
@@ -105,4 +105,4 @@ | ||
find<T extends BlockSuiteSelectionType>(type: T) { | ||
return this.value.find((sel): sel is BlockSuiteSelectionInstance[T] => | ||
find<T extends BlockSuite.SelectionType>(type: T) { | ||
return this.value.find((sel): sel is BlockSuite.SelectionInstance[T] => | ||
sel.is(type) | ||
@@ -112,4 +112,4 @@ ); | ||
filter<T extends BlockSuiteSelectionType>(type: T) { | ||
return this.value.filter((sel): sel is BlockSuiteSelectionInstance[T] => | ||
filter<T extends BlockSuite.SelectionType>(type: T) { | ||
return this.value.filter((sel): sel is BlockSuite.SelectionInstance[T] => | ||
sel.is(type) | ||
@@ -116,0 +116,0 @@ ); |
@@ -30,5 +30,8 @@ import { PathFinder } from '../../utils/index.js'; | ||
declare global { | ||
interface BlockSuiteSelection { | ||
block: typeof BlockSelection; | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace BlockSuite { | ||
interface Selection { | ||
block: typeof BlockSelection; | ||
} | ||
} | ||
} |
@@ -6,7 +6,10 @@ export * from './block.js'; | ||
declare global { | ||
type BlockSuiteSelectionType = keyof BlockSuiteSelection; | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace BlockSuite { | ||
type SelectionType = keyof Selection; | ||
type BlockSuiteSelectionInstance = { | ||
[P in BlockSuiteSelectionType]: InstanceType<BlockSuiteSelection[P]>; | ||
}; | ||
type SelectionInstance = { | ||
[P in SelectionType]: InstanceType<Selection[P]>; | ||
}; | ||
} | ||
} |
@@ -52,5 +52,8 @@ import { BaseSelection } from '../base.js'; | ||
declare global { | ||
interface BlockSuiteSelection { | ||
surface: typeof SurfaceSelection; | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace BlockSuite { | ||
interface Selection { | ||
surface: typeof SurfaceSelection; | ||
} | ||
} | ||
} |
@@ -86,5 +86,8 @@ import { PathFinder } from '../../utils/path-finder.js'; | ||
declare global { | ||
interface BlockSuiteSelection { | ||
text: typeof TextSelection; | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace BlockSuite { | ||
interface Selection { | ||
text: typeof TextSelection; | ||
} | ||
} | ||
} |
@@ -7,3 +7,3 @@ import type { BlockSchemaType } from '@blocksuite/store'; | ||
ComponentType = unknown, | ||
WidgetNames extends string = string | ||
WidgetNames extends string = string, | ||
> { | ||
@@ -16,3 +16,3 @@ component: ComponentType; | ||
ComponentType = unknown, | ||
WidgetNames extends string = string | ||
WidgetNames extends string = string, | ||
> { | ||
@@ -19,0 +19,0 @@ schema: BlockSchemaType; |
@@ -10,7 +10,5 @@ import { assertExists } from '@blocksuite/global/utils'; | ||
view: T; | ||
type: BlockSuite.ViewType; | ||
}; | ||
export type NodeViewLeaf<T> = NodeView<T> & { | ||
type: keyof BlockSuiteView; | ||
}; | ||
export type NodeViewTree<T> = NodeViewLeaf<T> & { | ||
export type NodeViewTree<T> = NodeView<T> & { | ||
children: NodeViewTree<T>[]; | ||
@@ -23,2 +21,3 @@ }; | ||
export interface BlockSuiteViewSpec<T = any> { | ||
type: BlockSuite.ViewType; | ||
fromDOM: (node: Node) => null | NodeView<T>; | ||
@@ -36,5 +35,5 @@ toDOM: (nodeView: NodeView<T>) => Element; | ||
private _cachedTree: NodeViewTree<NodeViewType> | null = null; | ||
private _cachedPath: Map<Node, NodeViewLeaf<NodeViewType>[]> = new Map(); | ||
private _cachedPath: Map<Node, NodeView<NodeViewType>[]> = new Map(); | ||
private _observer: MutationObserver; | ||
readonly viewSpec = new Map<string, BlockSuiteViewSpec>(); | ||
readonly viewSpec = new Set<BlockSuiteViewSpec>(); | ||
@@ -56,14 +55,13 @@ constructor(public blockStore: BlockStore) { | ||
register<T extends keyof BlockSuiteView>(type: T, spec: BlockSuiteView[T]) { | ||
this.viewSpec.set(type, spec); | ||
register<T extends BlockSuite.ViewType>(spec: BlockSuite.View[T]) { | ||
this.viewSpec.add(spec); | ||
} | ||
getNodeView = (node: Node): NodeViewLeaf<NodeViewType> | null => { | ||
for (const [type, spec] of this.viewSpec.entries()) { | ||
getNodeView = (node: Node): NodeView<NodeViewType> | null => { | ||
for (const [_, spec] of this.viewSpec.entries()) { | ||
const view = spec.fromDOM(node); | ||
if (view) { | ||
return { | ||
type, | ||
...view, | ||
} as NodeViewLeaf<NodeViewType>; | ||
} as NodeView<NodeViewType>; | ||
} | ||
@@ -75,9 +73,13 @@ } | ||
calculatePath = (node: Node) => { | ||
const path = this.calculateNodeViewPath(node); | ||
const path = this._calculateNodeViewPath(node); | ||
return path.map(x => x.id); | ||
}; | ||
calculateNodeViewPath = (node: Node) => { | ||
private _getViewSpec = (type: string) => { | ||
return Array.from(this.viewSpec).find(spec => spec.type === type); | ||
}; | ||
private _calculateNodeViewPath = (node: Node) => { | ||
if (this._cachedPath.has(node)) { | ||
return this._cachedPath.get(node) as NodeViewLeaf<NodeViewType>[]; | ||
return this._cachedPath.get(node) as NodeView<NodeViewType>[]; | ||
} | ||
@@ -88,4 +90,4 @@ const root = this.blockStore.root; | ||
node: Node | null, | ||
path: Array<NodeViewLeaf<NodeViewType>> | ||
): Array<NodeViewLeaf<NodeViewType>> => { | ||
path: Array<NodeView<NodeViewType>> | ||
): Array<NodeView<NodeViewType>> => { | ||
if (!node || node === root) return path; | ||
@@ -96,3 +98,3 @@ const nodeView = this.getNodeView(node); | ||
} | ||
const spec = this.viewSpec.get(nodeView.type); | ||
const spec = this._getViewSpec(nodeView.type); | ||
assertExists(spec); | ||
@@ -122,3 +124,3 @@ const next = spec.toDOM(nodeView as never).parentElement; | ||
const spec = this.viewSpec.get(nodeView.type); | ||
const spec = this._getViewSpec(nodeView.type); | ||
assertExists(spec); | ||
@@ -161,6 +163,6 @@ | ||
viewFromPath<T extends keyof BlockSuiteView>( | ||
viewFromPath<T extends BlockSuite.ViewType>( | ||
type: T, | ||
path: string[] | ||
): null | SpecToNodeView<BlockSuiteView[T]>; | ||
): null | SpecToNodeView<BlockSuite.View[T]>; | ||
viewFromPath<T extends BlockSuiteViewSpec>( | ||
@@ -333,2 +335,3 @@ type: string, | ||
} | ||
unmount() { | ||
@@ -347,4 +350,9 @@ this._cachedPath.clear(); | ||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface BlockSuiteView {} | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace BlockSuite { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface View {} | ||
type ViewType = keyof View; | ||
} | ||
} |
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
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
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3522
258454
+ Added@blocksuite/global@0.0.0-20230830111255-92eab248-nightly(transitive)
+ Added@blocksuite/store@0.0.0-20230830111255-92eab248-nightly(transitive)
+ Added@blocksuite/virgo@0.0.0-20230830111255-92eab248-nightly(transitive)
- Removed@blocksuite/global@0.0.0-20230829150056-df43987c-nightly(transitive)
- Removed@blocksuite/store@0.0.0-20230829150056-df43987c-nightly(transitive)
- Removed@blocksuite/virgo@0.0.0-20230829150056-df43987c-nightly(transitive)
- Removedky@0.33.3(transitive)
Updated@blocksuite/global@0.0.0-20230830111255-92eab248-nightly