Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@blocksuite/store

Package Overview
Dependencies
Maintainers
5
Versions
1249
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blocksuite/store - npm Package Compare versions

Comparing version 0.3.0-20221227082323-f18207d to 0.3.0-20221228081200-60d8fd8

dist/yjs/index.d.ts

16

dist/space.d.ts

@@ -5,2 +5,4 @@ import type * as Y from 'yjs';

import type { RichTextAdapter } from './text-adapter.js';
import type { DataInitializer } from './yjs/proxy.js';
import type { BlockSuiteDoc } from './yjs/index.js';
export interface StackItem {

@@ -10,9 +12,17 @@ meta: Map<'cursor-location', SelectionRange | undefined>;

}
export declare class Space {
export declare class Space<Data extends Record<string, unknown> = Record<string, any>> {
/** unprefixed id */
readonly id: string;
readonly doc: Y.Doc;
readonly doc: BlockSuiteDoc;
/**
* @internal
* @protected
*/
protected readonly proxy: Data;
protected readonly origin: Y.Map<Data[keyof Data]>;
readonly awareness: AwarenessAdapter;
readonly richTextAdapters: Map<string, RichTextAdapter>;
constructor(id: string, doc: Y.Doc, awareness: Awareness);
constructor(id: string, doc: BlockSuiteDoc, awareness: Awareness, options?: {
valueInitializer?: DataInitializer<Partial<Data>>;
});
get prefixedId(): string;

@@ -19,0 +29,0 @@ transact(fn: () => void): void;

import { Awareness } from 'y-protocols/awareness.js';
import { AwarenessAdapter } from './awareness.js';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Space {
constructor(id, doc, awareness) {
constructor(id, doc, awareness, options) {
this.richTextAdapters = new Map();
this.id = id;
this.doc = doc;
const targetId = this.id.startsWith('space:') ? this.id : this.prefixedId;
this.origin = this.doc.getMap(targetId);
this.proxy = this.doc.getMapProxy(targetId, {
initializer: options?.valueInitializer,
});
const aware = awareness ?? new Awareness(this.doc);

@@ -9,0 +15,0 @@ this.awareness = new AwarenessAdapter(this, aware);

6

dist/store.d.ts
import type { Space } from './space.js';
import type { IdGenerator } from './utils/id-generator.js';
import { Awareness } from 'y-protocols/awareness.js';
import * as Y from 'yjs';
import type { DocProvider, DocProviderConstructor } from './doc-providers.js';
import { BlockSuiteDoc } from './yjs/index.js';
export interface SerializedStore {

@@ -45,5 +45,5 @@ [key: string]: {

export declare class Store {
readonly doc: Y.Doc;
readonly doc: BlockSuiteDoc<import("./yjs/index.js").BlockSuiteDocData>;
readonly providers: DocProvider[];
readonly spaces: Map<string, Space>;
readonly spaces: Map<string, Space<Record<string, any>>>;
readonly awareness: Awareness;

@@ -50,0 +50,0 @@ readonly idGenerator: IdGenerator;

import { Awareness } from 'y-protocols/awareness.js';
import * as Y from 'yjs';
import { serializeYDoc, yDocToJSXNode } from './utils/jsx.js';
import { createAutoIncrementIdGenerator, createAutoIncrementIdGeneratorByClientId, uuidv4, } from './utils/id-generator.js';
import { BlockSuiteDoc } from './yjs/index.js';
export var Generator;

@@ -28,3 +28,3 @@ (function (Generator) {

constructor({ room = DEFAULT_ROOM, providers = [], awareness, idGenerator, } = {}) {
this.doc = new Y.Doc();
this.doc = new BlockSuiteDoc();
this.providers = [];

@@ -31,0 +31,0 @@ this.spaces = new Map();

@@ -10,2 +10,3 @@ import * as Y from 'yjs';

import type { PageMeta, Workspace } from './workspace.js';
import type { BlockSuiteDoc } from '../yjs/index.js';
export type YBlock = Y.Map<unknown>;

@@ -24,3 +25,6 @@ export type YBlocks = Y.Map<YBlock>;

};
export declare class Page extends Space {
export type PageData = {
[key: string]: YBlock;
};
export declare class Page extends Space<PageData> {
workspace: Workspace;

@@ -41,3 +45,3 @@ private _idGenerator;

};
constructor(workspace: Workspace, id: string, doc: Y.Doc, awareness: Awareness, idGenerator?: IdGenerator);
constructor(workspace: Workspace, id: string, doc: BlockSuiteDoc, awareness: Awareness, idGenerator?: IdGenerator);
get meta(): PageMeta;

@@ -44,0 +48,0 @@ get blobs(): Promise<import("../index.js").BlobStorage | null>;

@@ -65,3 +65,3 @@ import * as Y from 'yjs';

get _yBlocks() {
return this.doc.getMap(this.prefixedId);
return this.origin;
}

@@ -424,3 +424,3 @@ get root() {

if (this._yBlocks.size === 0) {
this.workspace.meta.writeVersion();
this.workspace.meta.writeVersion(this.workspace);
}

@@ -427,0 +427,0 @@ // Initialization from existing yDoc, indicating that the document is loaded from storage.

@@ -10,2 +10,3 @@ import * as Y from 'yjs';

import { BlobStorage } from '../blob/index.js';
import type { BlockSuiteDoc } from '../yjs/index.js';
export interface PageMeta {

@@ -17,4 +18,9 @@ id: string;

}
declare class WorkspaceMeta extends Space {
private _workspace;
type WorkspaceMetaData = {
pages: Y.Array<unknown>;
versions: Y.Map<unknown>;
name: string;
avatar: string;
};
declare class WorkspaceMeta extends Space<WorkspaceMetaData> {
private _prevPages;

@@ -25,16 +31,12 @@ pageAdded: Signal<string>;

commonFieldsUpdated: Signal<void>;
constructor(id: string, workspace: Workspace, awareness: Awareness);
private get _yMetaRoot();
private get _yPages();
private get _yVersions();
private get _yName();
private get _yAvatar();
constructor(id: string, doc: BlockSuiteDoc, awareness: Awareness);
get pages(): Y.Array<unknown>;
get name(): string;
get avatar(): string;
setName(val: string): void;
setAvatar(val: string): void;
setName(name: string): void;
setAvatar(avatar: string): void;
get pageMetas(): PageMeta[];
getPageMeta(id: string): PageMeta | undefined;
addPage(page: PageMeta, index?: number): void;
setPage(id: string, props: Partial<PageMeta>): void;
addPageMeta(page: PageMeta, index?: number): void;
setPageMeta(id: string, props: Partial<PageMeta>): void;
removePage(id: string): void;

@@ -44,3 +46,3 @@ /**

*/
writeVersion(): void;
writeVersion(workspace: Workspace): void;
/**

@@ -71,3 +73,3 @@ * @internal Only for page initialization

private get _pages();
get doc(): Y.Doc;
get doc(): BlockSuiteDoc<import("../yjs/index.js").BlockSuiteDocData>;
register(blockSchema: Record<string, typeof BaseBlockModel>): this;

@@ -74,0 +76,0 @@ private _hasPage;

@@ -9,4 +9,11 @@ import * as Y from 'yjs';

class WorkspaceMeta extends Space {
constructor(id, workspace, awareness) {
super(id, workspace.doc, awareness);
constructor(id, doc, awareness) {
super(id, doc, awareness, {
valueInitializer: {
pages: () => new Y.Array(),
versions: () => new Y.Map(),
avatar: () => '',
name: () => '',
},
});
this._prevPages = new Set();

@@ -19,5 +26,5 @@ this.pageAdded = new Signal();

events.forEach(e => {
const hasKey = (k) => e.target === this._yMetaRoot && e.changes.keys.has(k);
if (e.target === this._yPages ||
e.target.parent === this._yPages ||
const hasKey = (k) => e.target === this.origin && e.changes.keys.has(k);
if (e.target === this.pages ||
e.target.parent === this.pages ||
hasKey('pages')) {

@@ -31,50 +38,25 @@ this._handlePageEvent();

};
this._workspace = workspace;
this._yMetaRoot.observeDeep(this._handleEvents);
this.origin.observeDeep(this._handleEvents);
}
get _yMetaRoot() {
return this.doc.getMap(this.id);
get pages() {
return this.proxy.pages;
}
get _yPages() {
if (!this._yMetaRoot.has('pages')) {
this._yMetaRoot.set('pages', new Y.Array());
}
return this._yMetaRoot.get('pages');
}
get _yVersions() {
if (!this._yMetaRoot.has('versions')) {
this._yMetaRoot.set('versions', new Y.Map());
}
return this._yMetaRoot.get('versions');
}
get _yName() {
if (!this._yMetaRoot.has('name')) {
return null;
}
return this._yMetaRoot.get('name');
}
get _yAvatar() {
if (!this._yMetaRoot.has('avatar')) {
return null;
}
return this._yMetaRoot.get('avatar');
}
get name() {
return this._yName ? this._yName.toString() : '';
return this.proxy.name;
}
get avatar() {
return this._yAvatar ? this._yAvatar.toString() : '';
return this.proxy.avatar;
}
setName(val) {
setName(name) {
this.doc.transact(() => {
this._yMetaRoot.set('name', val);
this.proxy.name = name;
});
}
setAvatar(val) {
setAvatar(avatar) {
this.doc.transact(() => {
this._yMetaRoot.set('avatar', val);
this.proxy.avatar = avatar;
});
}
get pageMetas() {
return this._yPages.toJSON();
return this.proxy.pages.toJSON();
}

@@ -84,18 +66,18 @@ getPageMeta(id) {

}
addPage(page, index) {
addPageMeta(page, index) {
const yPage = new Y.Map();
this.doc.transact(() => {
Object.entries(page).forEach(([key, value]) => {
yPage.set(key, value);
});
if (index === undefined) {
this._yPages.push([yPage]);
Object.entries(page).forEach(([key, value]) => {
yPage.set(key, value);
});
this.pages.push([yPage]);
}
else {
this._yPages.insert(index, [yPage]);
this.pages.insert(index, [yPage]);
}
});
}
setPage(id, props) {
const pages = this._yPages.toJSON();
setPageMeta(id, props) {
const pages = this.pages.toJSON();
const index = pages.findIndex((page) => id === page.id);

@@ -105,3 +87,3 @@ this.doc.transact(() => {

return;
const yPage = this._yPages.get(index);
const yPage = this.pages.get(index);
Object.entries(props).forEach(([key, value]) => {

@@ -113,7 +95,7 @@ yPage.set(key, value);

removePage(id) {
const pages = this._yPages.toJSON();
const pages = this.pages.toJSON();
const index = pages.findIndex((page) => id === page.id);
this.doc.transact(() => {
if (index !== -1) {
this._yPages.delete(index, 1);
this.pages.delete(index, 1);
}

@@ -125,9 +107,9 @@ });

*/
writeVersion() {
const { _yVersions, _workspace } = this;
_workspace.flavourMap.forEach((model, flavour) => {
writeVersion(workspace) {
const versions = this.proxy.versions;
workspace.flavourMap.forEach((model, flavour) => {
const yVersion = new Y.Array();
const [major, minor] = model.version;
yVersion.push([major, minor]);
_yVersions.set(flavour, yVersion);
versions.set(flavour, yVersion);
});

@@ -179,3 +161,3 @@ }

this.room = options.room;
this.meta = new WorkspaceMeta('space:meta', this, this._store.awareness);
this.meta = new WorkspaceMeta('space:meta', this.doc, this._store.awareness);
this.signals = {

@@ -235,3 +217,3 @@ pagesUpdated: this.meta.pagesUpdated,

}
this.meta.addPage({
this.meta.addPageMeta({
id: pageId,

@@ -244,3 +226,3 @@ title: '',

setPageMeta(pageId, props) {
this.meta.setPage(pageId, props);
this.meta.setPageMeta(pageId, props);
}

@@ -247,0 +229,0 @@ removePage(pageId) {

{
"name": "@blocksuite/store",
"version": "0.3.0-20221227082323-f18207d",
"version": "0.3.0-20221228081200-60d8fd8",
"description": "BlockSuite data store built for general purpose state management.",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -5,2 +5,4 @@ import type * as Y from 'yjs';

import type { RichTextAdapter } from './text-adapter.js';
import type { DataInitializer } from './yjs/proxy.js';
import type { BlockSuiteDoc } from './yjs/index.js';

@@ -12,15 +14,34 @@ export interface StackItem {

export class Space {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Space<Data extends Record<string, unknown> = Record<string, any>> {
/** unprefixed id */
readonly id: string;
readonly doc: Y.Doc;
readonly doc: BlockSuiteDoc;
/**
* @internal
* @protected
*/
protected readonly proxy: Data;
protected readonly origin: Y.Map<Data[keyof Data]>;
readonly awareness!: AwarenessAdapter;
readonly richTextAdapters = new Map<string, RichTextAdapter>();
constructor(id: string, doc: Y.Doc, awareness: Awareness) {
constructor(
id: string,
doc: BlockSuiteDoc,
awareness: Awareness,
options?: {
valueInitializer?: DataInitializer<Partial<Data>>;
}
) {
this.id = id;
this.doc = doc;
const targetId = this.id.startsWith('space:') ? this.id : this.prefixedId;
this.origin = this.doc.getMap(targetId);
this.proxy = this.doc.getMapProxy<string, Data>(targetId, {
initializer: options?.valueInitializer,
});
const aware = awareness ?? new Awareness(this.doc);
this.awareness = new AwarenessAdapter(this, aware);
this.awareness = new AwarenessAdapter(this as Space, aware);
}

@@ -27,0 +48,0 @@

import type { Space } from './space.js';
import type { IdGenerator } from './utils/id-generator.js';
import { Awareness } from 'y-protocols/awareness.js';
import * as Y from 'yjs';
import type { DocProvider, DocProviderConstructor } from './doc-providers.js';

@@ -12,2 +11,3 @@ import { serializeYDoc, yDocToJSXNode } from './utils/jsx.js';

} from './utils/id-generator.js';
import { BlockSuiteDoc } from './yjs/index.js';

@@ -59,3 +59,3 @@ export interface SerializedStore {

export class Store {
readonly doc = new Y.Doc();
readonly doc = new BlockSuiteDoc();
readonly providers: DocProvider[] = [];

@@ -62,0 +62,0 @@ readonly spaces = new Map<string, Space>();

@@ -24,2 +24,3 @@ import * as Y from 'yjs';

import type { PageMeta, Workspace } from './workspace.js';
import type { BlockSuiteDoc } from '../yjs/index.js';

@@ -49,3 +50,7 @@ export type YBlock = Y.Map<unknown>;

export class Page extends Space {
export type PageData = {
[key: string]: YBlock;
};
export class Page extends Space<PageData> {
public workspace: Workspace;

@@ -75,3 +80,3 @@ private _idGenerator: IdGenerator;

id: string,
doc: Y.Doc,
doc: BlockSuiteDoc,
awareness: Awareness,

@@ -94,4 +99,4 @@ idGenerator: IdGenerator = uuidv4

/** key-value store of blocks */
private get _yBlocks() {
return this.doc.getMap(this.prefixedId) as YBlocks;
private get _yBlocks(): YBlocks {
return this.origin;
}

@@ -558,3 +563,3 @@

if (this._yBlocks.size === 0) {
this.workspace.meta.writeVersion();
this.workspace.meta.writeVersion(this.workspace);
}

@@ -561,0 +566,0 @@ // Initialization from existing yDoc, indicating that the document is loaded from storage.

@@ -10,2 +10,3 @@ import * as Y from 'yjs';

import { BlobStorage, getBlobStorage } from '../blob/index.js';
import type { BlockSuiteDoc } from '../yjs/index.js';

@@ -19,4 +20,10 @@ export interface PageMeta {

class WorkspaceMeta extends Space {
private _workspace: Workspace;
type WorkspaceMetaData = {
pages: Y.Array<unknown>;
versions: Y.Map<unknown>;
name: string;
avatar: string;
};
class WorkspaceMeta extends Space<WorkspaceMetaData> {
private _prevPages = new Set<string>();

@@ -28,59 +35,35 @@ pageAdded = new Signal<string>();

constructor(id: string, workspace: Workspace, awareness: Awareness) {
super(id, workspace.doc, awareness);
this._workspace = workspace;
this._yMetaRoot.observeDeep(this._handleEvents);
constructor(id: string, doc: BlockSuiteDoc, awareness: Awareness) {
super(id, doc, awareness, {
valueInitializer: {
pages: () => new Y.Array(),
versions: () => new Y.Map(),
avatar: () => '',
name: () => '',
},
});
this.origin.observeDeep(this._handleEvents);
}
private get _yMetaRoot() {
return this.doc.getMap(this.id);
get pages() {
return this.proxy.pages;
}
private get _yPages() {
if (!this._yMetaRoot.has('pages')) {
this._yMetaRoot.set('pages', new Y.Array());
}
return this._yMetaRoot.get('pages') as Y.Array<unknown>;
}
private get _yVersions() {
if (!this._yMetaRoot.has('versions')) {
this._yMetaRoot.set('versions', new Y.Map());
}
return this._yMetaRoot.get('versions') as Y.Map<unknown>;
}
private get _yName() {
if (!this._yMetaRoot.has('name')) {
return null;
}
return this._yMetaRoot.get('name') as string;
}
private get _yAvatar() {
if (!this._yMetaRoot.has('avatar')) {
return null;
}
return this._yMetaRoot.get('avatar') as Y.Text;
}
get name() {
return this._yName ? this._yName.toString() : '';
return this.proxy.name;
}
get avatar() {
return this._yAvatar ? this._yAvatar.toString() : '';
return this.proxy.avatar;
}
setName(val: string) {
setName(name: string) {
this.doc.transact(() => {
this._yMetaRoot.set('name', val);
this.proxy.name = name;
});
}
setAvatar(val: string) {
setAvatar(avatar: string) {
this.doc.transact(() => {
this._yMetaRoot.set('avatar', val);
this.proxy.avatar = avatar;
});

@@ -90,3 +73,3 @@ }

get pageMetas() {
return this._yPages.toJSON() as PageMeta[];
return this.proxy.pages.toJSON() as PageMeta[];
}

@@ -98,12 +81,12 @@

addPage(page: PageMeta, index?: number) {
addPageMeta(page: PageMeta, index?: number) {
const yPage = new Y.Map();
this.doc.transact(() => {
Object.entries(page).forEach(([key, value]) => {
yPage.set(key, value);
});
if (index === undefined) {
this._yPages.push([yPage]);
Object.entries(page).forEach(([key, value]) => {
yPage.set(key, value);
});
this.pages.push([yPage]);
} else {
this._yPages.insert(index, [yPage]);
this.pages.insert(index, [yPage]);
}

@@ -113,4 +96,4 @@ });

setPage(id: string, props: Partial<PageMeta>) {
const pages = this._yPages.toJSON() as PageMeta[];
setPageMeta(id: string, props: Partial<PageMeta>) {
const pages = this.pages.toJSON() as PageMeta[];
const index = pages.findIndex((page: PageMeta) => id === page.id);

@@ -121,3 +104,3 @@

const yPage = this._yPages.get(index) as Y.Map<unknown>;
const yPage = this.pages.get(index) as Y.Map<unknown>;
Object.entries(props).forEach(([key, value]) => {

@@ -130,3 +113,3 @@ yPage.set(key, value);

removePage(id: string) {
const pages = this._yPages.toJSON() as PageMeta[];
const pages = this.pages.toJSON() as PageMeta[];
const index = pages.findIndex((page: PageMeta) => id === page.id);

@@ -136,3 +119,3 @@

if (index !== -1) {
this._yPages.delete(index, 1);
this.pages.delete(index, 1);
}

@@ -145,9 +128,9 @@ });

*/
writeVersion() {
const { _yVersions, _workspace } = this;
_workspace.flavourMap.forEach((model, flavour) => {
writeVersion(workspace: Workspace) {
const versions = this.proxy.versions;
workspace.flavourMap.forEach((model, flavour) => {
const yVersion = new Y.Array();
const [major, minor] = model.version;
yVersion.push([major, minor]);
_yVersions.set(flavour, yVersion);
versions.set(flavour, yVersion);
});

@@ -199,7 +182,7 @@ }

const hasKey = (k: string) =>
e.target === this._yMetaRoot && e.changes.keys.has(k);
e.target === this.origin && e.changes.keys.has(k);
if (
e.target === this._yPages ||
e.target.parent === this._yPages ||
e.target === this.pages ||
e.target.parent === this.pages ||
hasKey('pages')

@@ -244,3 +227,7 @@ ) {

this.meta = new WorkspaceMeta('space:meta', this, this._store.awareness);
this.meta = new WorkspaceMeta(
'space:meta',
this.doc,
this._store.awareness
);

@@ -320,3 +307,3 @@ this.signals = {

this.meta.addPage({
this.meta.addPageMeta({
id: pageId,

@@ -330,3 +317,3 @@ title: '',

setPageMeta(pageId: string, props: Partial<PageMeta>) {
this.meta.setPage(pageId, props);
this.meta.setPageMeta(pageId, props);
}

@@ -333,0 +320,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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc