@blocksuite/store
Advanced tools
Comparing version 0.4.0-20230113023110-28a7fdc to 0.4.0-20230113184550-93ac0f4
@@ -40,2 +40,4 @@ /// <reference types="@blocksuite/global" /> | ||
getFlag<Key extends keyof Flags>(field: Key): any; | ||
setReadonly(value: boolean): void; | ||
isReadonly(): boolean; | ||
getLocalCursor(): SelectionRange | undefined; | ||
@@ -42,0 +44,0 @@ getStates(): Map<number, AwarenessState>; |
import * as Y from 'yjs'; | ||
import { Signal } from './utils/signal.js'; | ||
import { assertExists } from './utils/utils.js'; | ||
import { merge } from 'merge'; | ||
export class AwarenessAdapter { | ||
@@ -49,6 +50,3 @@ constructor( | ||
if (upstreamFlags) { | ||
this.awareness.setLocalStateField('flags', { | ||
...defaultFlags, | ||
...upstreamFlags, | ||
}); | ||
this.awareness.setLocalStateField('flags', merge(defaultFlags, upstreamFlags)); | ||
} | ||
@@ -71,2 +69,9 @@ else { | ||
} | ||
setReadonly(value) { | ||
const flags = this.getFlag('readonly'); | ||
this.setFlag('readonly', { ...flags, [this.space.prefixedId]: value }); | ||
} | ||
isReadonly() { | ||
return this.getFlag('readonly')[this.space.prefixedId] ?? false; | ||
} | ||
getLocalCursor() { | ||
@@ -73,0 +78,0 @@ const states = this.awareness.getStates(); |
@@ -23,2 +23,4 @@ /// <reference types="@blocksuite/global" /> | ||
sourceId?: string; | ||
parentIndex?: number; | ||
depth?: number; | ||
constructor(page: Page, props: Pick<BlockSuiteInternal.IBaseBlockProps, 'id'>); | ||
@@ -25,0 +27,0 @@ firstChild(): BaseBlockModel<unknown> | null; |
@@ -44,3 +44,3 @@ /// <reference types="@blocksuite/global" /> | ||
idGenerator?: Generator; | ||
defaultFlags?: Flags; | ||
defaultFlags?: Partial<Flags>; | ||
} | ||
@@ -47,0 +47,0 @@ export declare class Store { |
@@ -90,2 +90,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
_transact(callback) { | ||
if (this._space.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const { _space, _shouldTransact } = this; | ||
@@ -92,0 +96,0 @@ _shouldTransact ? _space.transact(callback) : callback(); |
@@ -80,5 +80,11 @@ import * as Y from 'yjs'; | ||
get canUndo() { | ||
if (this.awareness.isReadonly()) { | ||
return false; | ||
} | ||
return this._history.canUndo(); | ||
} | ||
get canRedo() { | ||
if (this.awareness.isReadonly()) { | ||
return false; | ||
} | ||
return this._history.canRedo(); | ||
@@ -90,5 +96,13 @@ } | ||
undo() { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
this._history.undo(); | ||
} | ||
redo() { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
this._history.redo(); | ||
@@ -183,2 +197,5 @@ } | ||
addBlock(blockProps, parent, parentIndex) { | ||
if (this.awareness.isReadonly()) { | ||
throw new Error('cannot modify data in readonly mode'); | ||
} | ||
if (!blockProps.flavour) { | ||
@@ -216,2 +233,6 @@ throw new Error('Block props must contain flavour'); | ||
updateBlockById(id, props) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const model = this._blockMap.get(id); | ||
@@ -221,2 +242,6 @@ this.updateBlock(model, props); | ||
moveBlock(model, targetModel, top = true) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const currentParentModel = this.getParent(model); | ||
@@ -248,2 +273,6 @@ const nextParentModel = this.getParent(targetModel); | ||
updateBlock(model, props) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const yBlock = this._yBlocks.get(model.id); | ||
@@ -271,2 +300,6 @@ this.transact(() => { | ||
deleteBlockById(id) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const model = this._blockMap.get(id); | ||
@@ -276,2 +309,6 @@ this.deleteBlock(model); | ||
deleteBlock(model) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const parent = this.getParent(model); | ||
@@ -278,0 +315,0 @@ const index = parent?.children.indexOf(model) ?? -1; |
@@ -0,1 +1,2 @@ | ||
/// <reference types="@blocksuite/global" /> | ||
import * as Y from 'yjs'; | ||
@@ -23,3 +24,3 @@ import { StoreOptions } from '../store.js'; | ||
}; | ||
declare class WorkspaceMeta extends Space<WorkspaceMetaData> { | ||
declare class WorkspaceMeta<Flags extends Record<string, unknown> = BlockSuiteFlags> extends Space<WorkspaceMetaData, Flags> { | ||
private _prevPages; | ||
@@ -30,3 +31,3 @@ pageAdded: Signal<string>; | ||
commonFieldsUpdated: Signal<void>; | ||
constructor(id: string, doc: BlockSuiteDoc, awareness: Awareness, defaultFlags?: Record<string, boolean>); | ||
constructor(id: string, doc: BlockSuiteDoc, awareness: Awareness, defaultFlags?: Partial<Flags>); | ||
get pages(): Y.Array<unknown>; | ||
@@ -33,0 +34,0 @@ get name(): string; |
@@ -159,2 +159,3 @@ import * as Y from 'yjs'; | ||
enable_drag_handle: true, | ||
readonly: {}, | ||
}; | ||
@@ -161,0 +162,0 @@ export class Workspace { |
{ | ||
"name": "@blocksuite/store", | ||
"version": "0.4.0-20230113023110-28a7fdc", | ||
"version": "0.4.0-20230113184550-93ac0f4", | ||
"description": "BlockSuite data store built for general purpose state management.", | ||
@@ -11,3 +11,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@blocksuite/global": "0.4.0-20230113023110-28a7fdc", | ||
"@blocksuite/global": "0.4.0-20230113184550-93ac0f4", | ||
"@types/flexsearch": "^0.7.3", | ||
@@ -20,2 +20,3 @@ "@types/quill": "^1.3.7", | ||
"lib0": "^0.2.58", | ||
"merge": "^2.1.1", | ||
"y-protocols": "^1.0.5", | ||
@@ -22,0 +23,0 @@ "y-webrtc": "^10.2.3" |
@@ -7,2 +7,3 @@ import * as Y from 'yjs'; | ||
import { assertExists } from './utils/utils.js'; | ||
import { merge } from 'merge'; | ||
@@ -68,6 +69,6 @@ export interface SelectionRange { | ||
if (upstreamFlags) { | ||
this.awareness.setLocalStateField('flags', { | ||
...defaultFlags, | ||
...upstreamFlags, | ||
}); | ||
this.awareness.setLocalStateField( | ||
'flags', | ||
merge(defaultFlags, upstreamFlags) | ||
); | ||
} else { | ||
@@ -93,2 +94,11 @@ this.awareness.setLocalStateField('flags', { ...defaultFlags }); | ||
public setReadonly(value: boolean): void { | ||
const flags = this.getFlag('readonly'); | ||
this.setFlag('readonly', { ...flags, [this.space.prefixedId]: value }); | ||
} | ||
public isReadonly(): boolean { | ||
return this.getFlag('readonly')[this.space.prefixedId] ?? false; | ||
} | ||
public getLocalCursor(): SelectionRange | undefined { | ||
@@ -95,0 +105,0 @@ const states = this.awareness.getStates(); |
@@ -31,2 +31,5 @@ import type { Page } from './workspace/index.js'; | ||
parentIndex?: number; | ||
depth?: number; | ||
constructor( | ||
@@ -33,0 +36,0 @@ page: Page, |
@@ -55,3 +55,3 @@ import type { Space } from './space.js'; | ||
idGenerator?: Generator; | ||
defaultFlags?: Flags; | ||
defaultFlags?: Partial<Flags>; | ||
} | ||
@@ -58,0 +58,0 @@ |
@@ -143,2 +143,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
private _transact(callback: () => void) { | ||
if (this._space.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const { _space, _shouldTransact } = this; | ||
@@ -145,0 +149,0 @@ _shouldTransact ? _space.transact(callback) : callback(); |
@@ -115,2 +115,5 @@ import * as Y from 'yjs'; | ||
get canUndo() { | ||
if (this.awareness.isReadonly()) { | ||
return false; | ||
} | ||
return this._history.canUndo(); | ||
@@ -120,2 +123,5 @@ } | ||
get canRedo() { | ||
if (this.awareness.isReadonly()) { | ||
return false; | ||
} | ||
return this._history.canRedo(); | ||
@@ -129,2 +135,6 @@ } | ||
undo() { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
this._history.undo(); | ||
@@ -134,2 +144,6 @@ } | ||
redo() { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
this._history.redo(); | ||
@@ -265,2 +279,5 @@ } | ||
): string { | ||
if (this.awareness.isReadonly()) { | ||
throw new Error('cannot modify data in readonly mode'); | ||
} | ||
if (!blockProps.flavour) { | ||
@@ -307,2 +324,6 @@ throw new Error('Block props must contain flavour'); | ||
updateBlockById(id: string, props: Partial<BlockProps>) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const model = this._blockMap.get(id) as BaseBlockModel; | ||
@@ -313,2 +334,6 @@ this.updateBlock(model, props); | ||
moveBlock(model: BaseBlockModel, targetModel: BaseBlockModel, top = true) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const currentParentModel = this.getParent(model); | ||
@@ -340,2 +365,6 @@ const nextParentModel = this.getParent(targetModel); | ||
updateBlock<T extends Partial<BlockProps>>(model: BaseBlockModel, props: T) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const yBlock = this._yBlocks.get(model.id) as YBlock; | ||
@@ -369,2 +398,6 @@ | ||
deleteBlockById(id: string) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const model = this._blockMap.get(id) as BaseBlockModel; | ||
@@ -375,2 +408,6 @@ this.deleteBlock(model); | ||
deleteBlock(model: BaseBlockModel) { | ||
if (this.awareness.isReadonly()) { | ||
console.error('cannot modify data in readonly mode'); | ||
return; | ||
} | ||
const parent = this.getParent(model); | ||
@@ -377,0 +414,0 @@ const index = parent?.children.indexOf(model) ?? -1; |
@@ -26,3 +26,5 @@ import * as Y from 'yjs'; | ||
class WorkspaceMeta extends Space<WorkspaceMetaData> { | ||
class WorkspaceMeta< | ||
Flags extends Record<string, unknown> = BlockSuiteFlags | ||
> extends Space<WorkspaceMetaData, Flags> { | ||
private _prevPages = new Set<string>(); | ||
@@ -38,3 +40,3 @@ pageAdded = new Signal<string>(); | ||
awareness: Awareness, | ||
defaultFlags?: Record<string, boolean> | ||
defaultFlags?: Partial<Flags> | ||
) { | ||
@@ -218,5 +220,6 @@ super(id, doc, awareness, { | ||
const flagsPreset: BlockSuiteFlags = { | ||
const flagsPreset = { | ||
enable_drag_handle: true, | ||
} as const; | ||
readonly: {}, | ||
} satisfies BlockSuiteFlags; | ||
@@ -223,0 +226,0 @@ export class Workspace { |
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
474973
7814
12
+ Addedmerge@^2.1.1
+ Added@blocksuite/global@0.4.0-20230113184550-93ac0f4(transitive)
+ Addedmerge@2.1.1(transitive)
- Removed@blocksuite/global@0.4.0-20230113023110-28a7fdc(transitive)