@blocksuite/store
Advanced tools
Comparing version 0.4.0-20230115125610-fb61886 to 0.4.0-20230116190618-0dc5fd3
@@ -37,3 +37,3 @@ /// <reference types="@blocksuite/global" /> | ||
} | ||
export interface AwarenessMetadataMessage<Flags extends Record<string, unknown> = BlockSuiteFlags, Key extends keyof Flags = keyof Flags> { | ||
export interface AwarenessMetaMessage<Flags extends Record<string, unknown> = BlockSuiteFlags, Key extends keyof Flags = keyof Flags> { | ||
field: Key; | ||
@@ -40,0 +40,0 @@ value: Flags[Key]; |
@@ -6,2 +6,3 @@ /// <reference types="@blocksuite/global" /> | ||
import type { DeltaOperation } from 'quill'; | ||
import type * as Y from 'yjs'; | ||
interface StaticValue { | ||
@@ -22,2 +23,4 @@ _$litStatic$: string; | ||
children: BaseBlockModel[]; | ||
tags?: Y.Map<Y.Map<unknown>>; | ||
tagSchema?: Y.Map<unknown>; | ||
text?: TextType; | ||
@@ -24,0 +27,0 @@ sourceId?: string; |
@@ -12,3 +12,9 @@ import { AbstractType, Map, Text, Array } from 'yjs'; | ||
} | ||
const IGNORE_PROPS = ['sys:id', 'sys:flavour', 'sys:children']; | ||
const IGNORE_PROPS = [ | ||
'sys:id', | ||
'sys:flavour', | ||
'sys:children', | ||
'meta:tags', | ||
'meta:tagSchema', | ||
]; | ||
export function yDocToJSXNode(serializedDoc, nodeId) { | ||
@@ -15,0 +21,0 @@ if (!isValidRecord(serializedDoc)) { |
@@ -5,3 +5,3 @@ import type { BlockProps, PrefixedBlockProps, YBlock, YBlocks } from '../workspace/page.js'; | ||
export declare function assertValidChildren(yBlocks: YBlocks, props: Partial<BlockProps>): void; | ||
export declare function initSysProps(yBlock: YBlock, props: Partial<BlockProps>): void; | ||
export declare function initInternalProps(yBlock: YBlock, props: Partial<BlockProps>): void; | ||
export declare function syncBlockProps(yBlock: YBlock, props: Partial<BlockProps>, ignoredKeys: Set<string>): void; | ||
@@ -8,0 +8,0 @@ export declare function trySyncTextProp(splitSet: Set<Text | PrelimText>, yBlock: YBlock, text?: TextType | void): void; |
@@ -14,5 +14,9 @@ import * as Y from 'yjs'; | ||
} | ||
export function initSysProps(yBlock, props) { | ||
export function initInternalProps(yBlock, props) { | ||
yBlock.set('sys:id', props.id); | ||
yBlock.set('sys:flavour', props.flavour); | ||
if (props.flavour === 'affine:page') { | ||
yBlock.set('meta:tags', new Y.Map()); | ||
yBlock.set('meta:tagSchema', new Y.Map()); | ||
} | ||
const yChildren = new Y.Array(); | ||
@@ -19,0 +23,0 @@ yBlock.set('sys:children', yChildren); |
import * as Y from 'yjs'; | ||
import { initSysProps } from '../utils/utils.js'; | ||
import { initInternalProps } from '../utils/utils.js'; | ||
import { uuidv4 } from '../utils/id-generator.js'; | ||
@@ -65,3 +65,3 @@ // New migration should be added to the end of this list | ||
const id = uuidv4(); | ||
initSysProps(yBlock, { | ||
initInternalProps(yBlock, { | ||
id, | ||
@@ -68,0 +68,0 @@ flavour: 'affine:surface', |
@@ -12,2 +12,4 @@ /// <reference types="@blocksuite/global" /> | ||
import type { BlockSuiteDoc } from '../yjs/index.js'; | ||
import BlockTag = BlockSuiteInternal.BlockTag; | ||
import TagSchema = BlockSuiteInternal.TagSchema; | ||
export type YBlock = Y.Map<unknown>; | ||
@@ -47,2 +49,4 @@ export type YBlocks = Y.Map<YBlock>; | ||
get meta(): PageMeta; | ||
get tags(): Y.Map<Y.Map<unknown>>; | ||
get tagSchema(): Y.Map<unknown>; | ||
get blobs(): Promise<import("../index.js").BlobStorage | null>; | ||
@@ -64,2 +68,7 @@ /** key-value store of blocks */ | ||
resetHistory(): void; | ||
updateBlockTag<Tag extends BlockTag>(id: BaseBlockModel['id'], tag: Tag): void; | ||
getBlockTags(model: BaseBlockModel): Record<string, BlockTag>; | ||
getBlockTagByTagSchema(model: BaseBlockModel, schema: TagSchema): BlockTag | null; | ||
getTagSchema(id: TagSchema['id']): {} | null; | ||
setTagSchema(schema: TagSchema): unknown; | ||
getBlockById(id: string): BaseBlockModel<unknown> | null; | ||
@@ -66,0 +75,0 @@ getBlockByFlavour(blockFlavour: string): BaseBlockModel<unknown>[]; |
@@ -7,3 +7,3 @@ import * as Y from 'yjs'; | ||
import { Signal } from '../utils/signal.js'; | ||
import { assertValidChildren, initSysProps, syncBlockProps, trySyncTextProp, toBlockProps, } from '../utils/utils.js'; | ||
import { assertValidChildren, initInternalProps, syncBlockProps, trySyncTextProp, toBlockProps, } from '../utils/utils.js'; | ||
import { tryMigrate } from './migrations.js'; | ||
@@ -65,2 +65,12 @@ import { assertExists, matchFlavours } from '@blocksuite/global/utils'; | ||
} | ||
get tags() { | ||
assertExists(this.root); | ||
assertExists(this.root.flavour === 'affine:page'); | ||
return this.root.tags; | ||
} | ||
get tagSchema() { | ||
assertExists(this.root); | ||
assertExists(this.root.flavour === 'affine:page'); | ||
return this.root.tagSchema; | ||
} | ||
get blobs() { | ||
@@ -130,2 +140,36 @@ return this.workspace.blobs; | ||
} | ||
updateBlockTag(id, tag) { | ||
const already = this.tags.has(id); | ||
let tags; | ||
if (!already) { | ||
tags = new Y.Map(); | ||
} | ||
else { | ||
tags = this.tags.get(id); | ||
} | ||
this.transact(() => { | ||
if (!already) { | ||
this.tags.set(id, tags); | ||
} | ||
tags.set(tag.type, tag); | ||
}); | ||
} | ||
getBlockTags(model) { | ||
const tags = this.tags.get(model.id); | ||
if (!tags) { | ||
return {}; | ||
} | ||
// fixme: performance issue | ||
return tags.toJSON(); | ||
} | ||
getBlockTagByTagSchema(model, schema) { | ||
const tags = this.tags.get(model.id); | ||
return tags?.get(schema.id) ?? null; | ||
} | ||
getTagSchema(id) { | ||
return this.tagSchema.get(id) ?? null; | ||
} | ||
setTagSchema(schema) { | ||
return this.tagSchema.set(schema.id, schema); | ||
} | ||
getBlockById(id) { | ||
@@ -202,15 +246,6 @@ return this._blockMap.get(id) ?? null; | ||
addBlockByFlavour(flavour, blockProps = {}, parent, parentIndex) { | ||
return this.addBlock({ | ||
flavour, | ||
...blockProps, | ||
}, parent, parentIndex); | ||
} | ||
/** | ||
* @deprecated use `addBlockByFlavour` | ||
*/ | ||
addBlock(blockProps, parent, parentIndex) { | ||
if (this.awareness.isReadonly()) { | ||
throw new Error('cannot modify data in readonly mode'); | ||
} | ||
if (!blockProps.flavour) { | ||
if (!flavour) { | ||
throw new Error('Block props must contain flavour'); | ||
@@ -223,3 +258,3 @@ } | ||
// } | ||
const clonedProps = { ...blockProps }; | ||
const clonedProps = { flavour, ...blockProps }; | ||
const id = this._idGenerator(); | ||
@@ -230,3 +265,3 @@ clonedProps.id = id; | ||
assertValidChildren(this._yBlocks, clonedProps); | ||
initSysProps(yBlock, clonedProps); | ||
initInternalProps(yBlock, clonedProps); | ||
syncBlockProps(yBlock, clonedProps, this._ignoredKeys); | ||
@@ -248,2 +283,8 @@ trySyncTextProp(this._splitSet, yBlock, clonedProps.text); | ||
} | ||
/** | ||
* @deprecated use `addBlockByFlavour` | ||
*/ | ||
addBlock(blockProps, parent, parentIndex) { | ||
return this.addBlockByFlavour(blockProps.flavour, blockProps, parent, parentIndex); | ||
} | ||
updateBlockById(id, props) { | ||
@@ -472,2 +513,6 @@ if (this.awareness.isReadonly()) { | ||
model.text = text; | ||
if (model.flavour === 'affine:page') { | ||
model.tags = yBlock.get('meta:tags'); | ||
model.tagSchema = yBlock.get('meta:tags'); | ||
} | ||
const yChildren = yBlock.get('sys:children'); | ||
@@ -474,0 +519,0 @@ if (yChildren instanceof Y.Array) { |
import type { DocumentSearchOptions } from 'flexsearch'; | ||
import { Doc } from 'yjs'; | ||
export type QueryContent = string | Partial<DocumentSearchOptions<boolean>>; | ||
export type IndexMetadata = Readonly<{ | ||
export type IndexMeta = Readonly<{ | ||
content: string; | ||
@@ -6,0 +6,0 @@ reference?: string; |
@@ -18,3 +18,3 @@ /// <reference types="@blocksuite/global" /> | ||
} | ||
type WorkspaceMetaData = { | ||
type WorkspaceMetaFields = { | ||
pages: Y.Array<unknown>; | ||
@@ -25,3 +25,3 @@ versions: Y.Map<unknown>; | ||
}; | ||
declare class WorkspaceMeta<Flags extends Record<string, unknown> = BlockSuiteFlags> extends Space<WorkspaceMetaData, Flags> { | ||
declare class WorkspaceMeta<Flags extends Record<string, unknown> = BlockSuiteFlags> extends Space<WorkspaceMetaFields, Flags> { | ||
private _prevPages; | ||
@@ -28,0 +28,0 @@ pageAdded: Signal<string>; |
{ | ||
"name": "@blocksuite/store", | ||
"version": "0.4.0-20230115125610-fb61886", | ||
"version": "0.4.0-20230116190618-0dc5fd3", | ||
"description": "BlockSuite data store built for general purpose state management.", | ||
@@ -11,3 +11,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@blocksuite/global": "0.4.0-20230115125610-fb61886", | ||
"@blocksuite/global": "0.4.0-20230116190618-0dc5fd3", | ||
"@types/flexsearch": "^0.7.3", | ||
@@ -14,0 +14,0 @@ "buffer": "^6.0.3", |
@@ -102,2 +102,4 @@ /* eslint-disable @typescript-eslint/no-restricted-imports */ | ||
'0': { | ||
'meta:tags': {}, | ||
'meta:tagSchema': {}, | ||
'sys:children': [], | ||
@@ -116,2 +118,4 @@ 'sys:flavour': 'affine:page', | ||
'0': { | ||
'meta:tags': {}, | ||
'meta:tagSchema': {}, | ||
'sys:children': [], | ||
@@ -132,2 +136,4 @@ 'sys:flavour': 'affine:page', | ||
'0': { | ||
'meta:tags': {}, | ||
'meta:tagSchema': {}, | ||
'sys:children': ['1'], | ||
@@ -254,2 +260,4 @@ 'sys:flavour': 'affine:page', | ||
'0': { | ||
'meta:tags': {}, | ||
'meta:tagSchema': {}, | ||
'sys:children': [], | ||
@@ -274,2 +282,4 @@ 'sys:flavour': 'affine:page', | ||
'0': { | ||
'meta:tags': {}, | ||
'meta:tagSchema': {}, | ||
'sys:children': ['1'], | ||
@@ -293,2 +303,4 @@ 'sys:flavour': 'affine:page', | ||
'0': { | ||
'meta:tags': {}, | ||
'meta:tagSchema': {}, | ||
'sys:children': [], | ||
@@ -295,0 +307,0 @@ 'sys:flavour': 'affine:page', |
@@ -53,3 +53,3 @@ import * as Y from 'yjs'; | ||
export interface AwarenessMetadataMessage< | ||
export interface AwarenessMetaMessage< | ||
Flags extends Record<string, unknown> = BlockSuiteFlags, | ||
@@ -56,0 +56,0 @@ Key extends keyof Flags = keyof Flags |
@@ -5,2 +5,3 @@ import type { Page } from './workspace/index.js'; | ||
import type { DeltaOperation } from 'quill'; | ||
import type * as Y from 'yjs'; | ||
@@ -29,2 +30,4 @@ // ported from lit | ||
// TODO use schema | ||
tags?: Y.Map<Y.Map<unknown>>; | ||
tagSchema?: Y.Map<unknown>; | ||
text?: TextType; | ||
@@ -31,0 +34,0 @@ sourceId?: string; |
@@ -32,3 +32,9 @@ import { AbstractType, Doc, Map, Text, Array } from 'yjs'; | ||
const IGNORE_PROPS = ['sys:id', 'sys:flavour', 'sys:children']; | ||
const IGNORE_PROPS = [ | ||
'sys:id', | ||
'sys:flavour', | ||
'sys:children', | ||
'meta:tags', | ||
'meta:tagSchema', | ||
]; | ||
@@ -35,0 +41,0 @@ export function yDocToJSXNode( |
@@ -26,5 +26,9 @@ import * as Y from 'yjs'; | ||
export function initSysProps(yBlock: YBlock, props: Partial<BlockProps>) { | ||
export function initInternalProps(yBlock: YBlock, props: Partial<BlockProps>) { | ||
yBlock.set('sys:id', props.id); | ||
yBlock.set('sys:flavour', props.flavour); | ||
if (props.flavour === 'affine:page') { | ||
yBlock.set('meta:tags', new Y.Map()); | ||
yBlock.set('meta:tagSchema', new Y.Map()); | ||
} | ||
@@ -31,0 +35,0 @@ const yChildren = new Y.Array(); |
import * as Y from 'yjs'; | ||
import type { YBlock } from './page.js'; | ||
import { initSysProps } from '../utils/utils.js'; | ||
import { initInternalProps } from '../utils/utils.js'; | ||
import { uuidv4 } from '../utils/id-generator.js'; | ||
@@ -75,3 +75,3 @@ | ||
const id = uuidv4(); | ||
initSysProps(yBlock, { | ||
initInternalProps(yBlock, { | ||
id, | ||
@@ -78,0 +78,0 @@ flavour: 'affine:surface', |
@@ -17,3 +17,3 @@ import * as Y from 'yjs'; | ||
assertValidChildren, | ||
initSysProps, | ||
initInternalProps, | ||
syncBlockProps, | ||
@@ -27,2 +27,4 @@ trySyncTextProp, | ||
import { assertExists, matchFlavours } from '@blocksuite/global/utils'; | ||
import BlockTag = BlockSuiteInternal.BlockTag; | ||
import TagSchema = BlockSuiteInternal.TagSchema; | ||
export type YBlock = Y.Map<unknown>; | ||
@@ -94,2 +96,14 @@ export type YBlocks = Y.Map<YBlock>; | ||
get tags() { | ||
assertExists(this.root); | ||
assertExists(this.root.flavour === 'affine:page'); | ||
return this.root.tags as Y.Map<Y.Map<unknown>>; | ||
} | ||
get tagSchema() { | ||
assertExists(this.root); | ||
assertExists(this.root.flavour === 'affine:page'); | ||
return this.root.tagSchema as Y.Map<unknown>; | ||
} | ||
get blobs() { | ||
@@ -171,2 +185,43 @@ return this.workspace.blobs; | ||
updateBlockTag<Tag extends BlockTag>(id: BaseBlockModel['id'], tag: Tag) { | ||
const already = this.tags.has(id); | ||
let tags: Y.Map<unknown>; | ||
if (!already) { | ||
tags = new Y.Map(); | ||
} else { | ||
tags = this.tags.get(id) as Y.Map<unknown>; | ||
} | ||
this.transact(() => { | ||
if (!already) { | ||
this.tags.set(id, tags); | ||
} | ||
tags.set(tag.type, tag); | ||
}); | ||
} | ||
getBlockTags(model: BaseBlockModel): Record<string, BlockTag> { | ||
const tags = this.tags.get(model.id); | ||
if (!tags) { | ||
return {}; | ||
} | ||
// fixme: performance issue | ||
return tags.toJSON(); | ||
} | ||
getBlockTagByTagSchema( | ||
model: BaseBlockModel, | ||
schema: TagSchema | ||
): BlockTag | null { | ||
const tags = this.tags.get(model.id); | ||
return (tags?.get(schema.id) as BlockTag) ?? null; | ||
} | ||
getTagSchema(id: TagSchema['id']) { | ||
return this.tagSchema.get(id) ?? (null as TagSchema | null); | ||
} | ||
setTagSchema(schema: TagSchema) { | ||
return this.tagSchema.set(schema.id, schema); | ||
} | ||
getBlockById(id: string) { | ||
@@ -272,24 +327,6 @@ return this._blockMap.get(id) ?? null; | ||
) { | ||
return this.addBlock( | ||
{ | ||
flavour, | ||
...blockProps, | ||
}, | ||
parent, | ||
parentIndex | ||
); | ||
} | ||
/** | ||
* @deprecated use `addBlockByFlavour` | ||
*/ | ||
addBlock<T extends BlockProps>( | ||
blockProps: Partial<T>, | ||
parent?: BaseBlockModel | string | null, | ||
parentIndex?: number | ||
): string { | ||
if (this.awareness.isReadonly()) { | ||
throw new Error('cannot modify data in readonly mode'); | ||
} | ||
if (!blockProps.flavour) { | ||
if (!flavour) { | ||
throw new Error('Block props must contain flavour'); | ||
@@ -304,3 +341,3 @@ } | ||
const clonedProps: Partial<BlockProps> = { ...blockProps }; | ||
const clonedProps: Partial<BlockProps> = { flavour, ...blockProps }; | ||
const id = this._idGenerator(); | ||
@@ -313,3 +350,3 @@ clonedProps.id = id; | ||
assertValidChildren(this._yBlocks, clonedProps); | ||
initSysProps(yBlock, clonedProps); | ||
initInternalProps(yBlock, clonedProps); | ||
syncBlockProps(yBlock, clonedProps, this._ignoredKeys); | ||
@@ -336,2 +373,18 @@ trySyncTextProp(this._splitSet, yBlock, clonedProps.text); | ||
/** | ||
* @deprecated use `addBlockByFlavour` | ||
*/ | ||
addBlock<T extends BlockProps>( | ||
blockProps: Partial<T>, | ||
parent?: BaseBlockModel | string | null, | ||
parentIndex?: number | ||
): string { | ||
return this.addBlockByFlavour( | ||
blockProps.flavour as Parameters<typeof this.addBlockByFlavour>[0], | ||
blockProps as Parameters<typeof this.addBlockByFlavour>[1], | ||
parent, | ||
parentIndex | ||
); | ||
} | ||
updateBlockById(id: string, props: Partial<BlockProps>) { | ||
@@ -625,2 +678,6 @@ if (this.awareness.isReadonly()) { | ||
model.text = text; | ||
if (model.flavour === 'affine:page') { | ||
model.tags = yBlock.get('meta:tags') as Y.Map<Y.Map<unknown>>; | ||
model.tagSchema = yBlock.get('meta:tags') as Y.Map<unknown>; | ||
} | ||
@@ -627,0 +684,0 @@ const yChildren = yBlock.get('sys:children'); |
@@ -48,3 +48,3 @@ import FlexSearch from 'flexsearch'; | ||
export type IndexMetadata = Readonly<{ | ||
export type IndexMeta = Readonly<{ | ||
content: string; | ||
@@ -58,3 +58,3 @@ reference?: string; | ||
private readonly _doc: Doc; | ||
private readonly _indexer: FlexSearch.Document<IndexMetadata, string[]>; | ||
private readonly _indexer: FlexSearch.Document<IndexMeta, string[]>; | ||
@@ -67,3 +67,3 @@ constructor( | ||
this._doc = doc; | ||
this._indexer = new DocumentIndexer<IndexMetadata, string[]>({ | ||
this._indexer = new DocumentIndexer<IndexMeta, string[]>({ | ||
document: { | ||
@@ -70,0 +70,0 @@ id: 'id', |
@@ -20,3 +20,3 @@ import * as Y from 'yjs'; | ||
type WorkspaceMetaData = { | ||
type WorkspaceMetaFields = { | ||
pages: Y.Array<unknown>; | ||
@@ -30,3 +30,3 @@ versions: Y.Map<unknown>; | ||
Flags extends Record<string, unknown> = BlockSuiteFlags | ||
> extends Space<WorkspaceMetaData, Flags> { | ||
> extends Space<WorkspaceMetaFields, Flags> { | ||
private _prevPages = new Set<string>(); | ||
@@ -33,0 +33,0 @@ pageAdded = new Signal<string>(); |
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
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
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
493053
8168
+ Added@blocksuite/global@0.4.0-20230116190618-0dc5fd3(transitive)
- Removed@blocksuite/global@0.4.0-20230115125610-fb61886(transitive)