New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@blocksuite/store

Package Overview
Dependencies
Maintainers
5
Versions
1304
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.4.0-20230211035417-55e3edb to 0.4.0-20230212194855-047e1b9

4

dist/base.d.ts

@@ -5,3 +5,3 @@ /// <reference types="@blocksuite/global" />

import { z } from 'zod';
import type { TextType } from './text-adapter.js';
import type { Text } from './text-adapter.js';
import type { Page } from './workspace/index.js';

@@ -94,3 +94,3 @@ export declare const BlockSchema: z.ZodObject<{

tagSchema?: Y.Map<unknown>;
text?: TextType;
text?: Text;
sourceId?: string;

@@ -97,0 +97,0 @@ parentIndex?: number;

import type { DeltaOperation, Quill } from 'quill';
import * as Y from 'yjs';
import type { Space } from './space.js';
type PrelimTextType = 'splitLeft' | 'splitRight';
export type TextType = PrelimText | Text;
export declare function normQuillDelta(delta: DeltaOperation[]): DeltaOperation[];
export declare class PrelimText {
ready: boolean;
type: PrelimTextType;
index: number;
constructor(type: PrelimTextType, index: number);
get length(): number;
clone(): void;
insert(): void;
insertList(): void;
split(): void;
join(): void;
clear(): void;
delete(): void;
replace(): void;
format(): void;
applyDelta(): void;
sliceToDelta(): void;
}
declare module 'yjs' {

@@ -52,17 +32,23 @@ interface Text {

private _yText;
/**
* @internal
*/
delayedJobs: (() => void)[];
private _shouldTransact;
constructor(input: Y.Text | string);
/**
* @internal
*/
doDelayedJobs(): void;
static fromDelta(delta: DeltaOperation[]): Text;
get length(): number;
get yText(): Y.YText;
private _transact;
clone(): Text;
split(index: number, length: number): [PrelimText, PrelimText];
/**
* Here are three cases for point position(index + length):
* [{insert: 'abc', ...}, {insert: 'def', ...}, {insert: 'ghi', ...}]
* 1. abc|de|fghi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'f', ...}, {insert: 'ghi', ...}]
* 2. abc|def|ghi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'ghi', ...}]
* 3. abc|defg|hi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'hi', ...}]
*/
split(index: number, length: number): Text;
insert(content: string, index: number, attributes?: Record<string, unknown>): void;

@@ -97,3 +83,2 @@ insertList(insertTexts: DeltaOperation[], index: number): void;

}
export {};
//# sourceMappingURL=text-adapter.d.ts.map

@@ -24,52 +24,4 @@ import * as Y from 'yjs';

}
const UNSUPPORTED_MSG = 'PrelimText does not support ';
export class PrelimText {
constructor(type, index) {
this.ready = false;
this.type = type;
this.index = index;
}
get length() {
return 0;
}
clone() {
throw new Error(UNSUPPORTED_MSG + 'clone');
}
insert() {
throw new Error(UNSUPPORTED_MSG + 'insert');
}
insertList() {
throw new Error(UNSUPPORTED_MSG + 'insertList');
}
split() {
throw new Error(UNSUPPORTED_MSG + 'split');
}
join() {
throw new Error(UNSUPPORTED_MSG + 'join');
}
clear() {
throw new Error(UNSUPPORTED_MSG + 'clear');
}
delete() {
throw new Error(UNSUPPORTED_MSG + 'delete');
}
replace() {
throw new Error(UNSUPPORTED_MSG + 'replace');
}
format() {
throw new Error(UNSUPPORTED_MSG + 'format');
}
applyDelta() {
throw new Error(UNSUPPORTED_MSG + 'applyDelta');
}
sliceToDelta() {
throw new Error(UNSUPPORTED_MSG + 'sliceToDelta');
}
}
export class Text {
constructor(input) {
/**
* @internal
*/
this.delayedJobs = [];
// TODO toggle transact by options

@@ -84,14 +36,6 @@ this._shouldTransact = true;

}
/**
* @internal
*/
doDelayedJobs() {
this.delayedJobs.forEach(cb => cb());
this.delayedJobs = [];
}
static fromDelta(delta) {
const result = new Text('');
// In the first time, yDoc does not exist.
result.delayedJobs.push(() => result.applyDelta(delta));
return result;
const result = new Y.Text();
result.applyDelta(delta);
return new Text(result);
}

@@ -101,2 +45,5 @@ get length() {

}
get yText() {
return this._yText;
}
_transact(callback) {

@@ -119,7 +66,47 @@ if (this._shouldTransact) {

}
/**
* Here are three cases for point position(index + length):
* [{insert: 'abc', ...}, {insert: 'def', ...}, {insert: 'ghi', ...}]
* 1. abc|de|fghi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'f', ...}, {insert: 'ghi', ...}]
* 2. abc|def|ghi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'ghi', ...}]
* 3. abc|defg|hi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'hi', ...}]
*/
split(index, length) {
return [
new PrelimText('splitLeft', index),
new PrelimText('splitRight', index + length),
];
const deltas = this._yText.toDelta();
if (deltas instanceof Array) {
let tmpIndex = 0;
const rightDeltas = [];
for (let i = 0; i < deltas.length; i++) {
const insert = deltas[i].insert;
if (typeof insert === 'string') {
if (tmpIndex + insert.length >= index + length) {
const insertRight = insert.slice(index + length - tmpIndex);
rightDeltas.push({
insert: insertRight,
attributes: deltas[i].attributes,
});
rightDeltas.push(...deltas.slice(i + 1));
break;
}
tmpIndex += insert.length;
}
else {
throw new Error('This text cannot be split because it contains non-string insert.');
}
}
this.delete(index, this.length - index);
const rightYText = new Y.Text();
rightYText.applyDelta(rightDeltas);
const rightText = new Text(rightYText);
return rightText;
}
else {
throw new Error('This text cannot be split because we failed to get the deltas of it.');
}
}

@@ -126,0 +113,0 @@ insert(content, index, attributes) {

/// <reference types="@blocksuite/global" />
import type { BaseBlockModel } from '../base.js';
import { PrelimText, Text, TextType } from '../text-adapter.js';
import type { Workspace } from '../workspace/index.js';

@@ -10,3 +9,2 @@ import type { BlockProps, YBlock, YBlocks } from '../workspace/page.js';

export declare function syncBlockProps(defaultProps: Record<string, unknown>, yBlock: YBlock, props: Partial<BlockProps>, ignoredKeys: Set<string>): void;
export declare function trySyncTextProp(splitSet: Set<Text | PrelimText>, yBlock: YBlock, text?: TextType | void): void;
export declare function toBlockProps(yBlock: YBlock): Partial<BlockProps>;

@@ -13,0 +11,0 @@ export declare function encodeWorkspaceAsYjsUpdateV2(workspace: Workspace): string;

import { isPrimitive, matchFlavours, SYS_KEYS } from '@blocksuite/global/utils';
import { fromBase64, toBase64 } from 'lib0/buffer.js';
import * as Y from 'yjs';
import { PrelimText, Text } from '../text-adapter.js';
import { Text } from '../text-adapter.js';
export function assertValidChildren(yBlocks, props) {

@@ -35,4 +35,6 @@ if (!Array.isArray(props.children))

// TODO use schema
if (key === 'text')
if (key === 'text' && value instanceof Text) {
yBlock.set(`prop:${key}`, value.yText);
return;
}
if (!isPrimitive(value) && !Array.isArray(value)) {

@@ -62,43 +64,2 @@ throw new Error('Only top level primitives are supported for now');

}
export function trySyncTextProp(splitSet, yBlock, text) {
if (!text)
return;
// update by clone
if (text instanceof Text) {
// @ts-ignore
yBlock.set('prop:text', text._yText);
text.doDelayedJobs();
return;
}
// update by split
if (text instanceof PrelimText) {
const iter = splitSet.values();
const base = iter.next().value;
const left = iter.next().value;
const right = iter.next().value;
if (!left.ready) {
throw new Error('PrelimText left is not ready');
}
if (left.type !== 'splitLeft' ||
right.type !== 'splitRight' ||
right !== text) {
throw new Error('Unmatched text entity');
}
// @ts-ignore
const yBase = base._yText;
// attach meta state for identifying split
// otherwise local change from y-side will be ignored by TextAdapter
// @ts-ignore
yBase.meta = { split: true };
// clone the original text to `yRight` and add it to the doc first
const yRight = yBase.clone();
yBlock.set('prop:text', yRight);
// delete the left-half part of `yRight`, making it the new right
yRight.delete(0, right.index);
// delete the right-half part of `yBase`, making it the new left
yBase.delete(left.index, yBase.length - left.index);
// cleanup
splitSet.clear();
}
}
export function toBlockProps(yBlock) {

@@ -105,0 +66,0 @@ const prefixedProps = yBlock.toJSON();

@@ -9,3 +9,3 @@ /// <reference types="@blocksuite/global" />

import { Space } from '../space.js';
import { PrelimText, Text, TextType } from '../text-adapter.js';
import { Text } from '../text-adapter.js';
import type { IdGenerator } from '../utils/id-generator.js';

@@ -20,3 +20,3 @@ import type { BlockSuiteDoc } from '../yjs/index.js';

flavour: string;
text?: void | TextType;
text?: Text;
children?: BaseBlockModel[];

@@ -37,3 +37,2 @@ };

private _blockMap;
private _splitSet;
private _synced;

@@ -73,3 +72,3 @@ private _ignoredKeys;

getTagSchema(id: TagSchema['id']): TagSchema | null;
setTagSchema(schema: TagSchema): void;
setTagSchema(schema: Omit<TagSchema, 'id'>): string;
getBlockById(id: string): BaseBlockModel<unknown> | null;

@@ -100,3 +99,2 @@ getBlockByFlavour(blockFlavour: string): BaseBlockModel<unknown>[];

detachRichText(id: string): void;
markTextSplit(base: Text, left: PrelimText, right: PrelimText): void;
syncFromExistingDoc(): void;

@@ -103,0 +101,0 @@ dispose(): void;

@@ -14,4 +14,4 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {

import { Space } from '../space.js';
import { PrelimText, RichTextAdapter, Text, } from '../text-adapter.js';
import { assertValidChildren, initInternalProps, syncBlockProps, toBlockProps, trySyncTextProp, } from '../utils/utils.js';
import { RichTextAdapter, Text } from '../text-adapter.js';
import { assertValidChildren, initInternalProps, syncBlockProps, toBlockProps, } from '../utils/utils.js';
import { tryMigrate } from './migrations.js';

@@ -27,3 +27,2 @@ const isWeb = typeof window !== 'undefined';

this._blockMap = new Map();
this._splitSet = new Set();
this._synced = false;

@@ -204,3 +203,5 @@ // TODO use schema

setTagSchema(schema) {
this.transact(() => this.tagSchema.set(schema.id, schema));
const id = this._idGenerator();
this.transact(() => this.tagSchema.set(id, { ...schema, id }));
return id;
}

@@ -300,3 +301,2 @@ getBlockById(id) {

syncBlockProps(defaultProps, yBlock, clonedProps, this._ignoredKeys);
trySyncTextProp(this._splitSet, yBlock, clonedProps.text);
if (typeof parent === 'string') {

@@ -366,9 +366,5 @@ parent = this._blockMap.get(parent);

this.transact(() => {
if (props.text instanceof PrelimText) {
props.text.ready = true;
}
else if (props.text instanceof Text) {
if (props.text instanceof Text) {
model.text = props.text;
// @ts-ignore
yBlock.set('prop:text', props.text._yText);
yBlock.set('prop:text', props.text.yText);
}

@@ -453,5 +449,2 @@ // TODO diff children changes

}
markTextSplit(base, left, right) {
this._splitSet.add(base).add(left).add(right);
}
syncFromExistingDoc() {

@@ -458,0 +451,0 @@ if (this._synced) {

{
"name": "@blocksuite/store",
"version": "0.4.0-20230211035417-55e3edb",
"version": "0.4.0-20230212194855-047e1b9",
"description": "BlockSuite data store built for general purpose state management.",

@@ -11,3 +11,4 @@ "main": "dist/index.js",

"dependencies": {
"@blocksuite/global": "0.4.0-20230211035417-55e3edb",
"@blocksuite/global": "0.4.0-20230212194855-047e1b9",
"@blocksuite/virgo": "0.4.0-20230212194855-047e1b9",
"@types/flexsearch": "^0.7.3",

@@ -14,0 +15,0 @@ "buffer": "^6.0.3",

@@ -5,3 +5,3 @@ import { Signal } from '@blocksuite/global/utils';

import type { TextType } from './text-adapter.js';
import type { Text } from './text-adapter.js';
import type { Page } from './workspace/index.js';

@@ -99,3 +99,3 @@

tagSchema?: Y.Map<unknown>;
text?: TextType;
text?: Text;
sourceId?: string;

@@ -102,0 +102,0 @@

/* eslint-disable @typescript-eslint/no-explicit-any */
import type { DeltaInsert } from '@blocksuite/virgo';
import type { DeltaOperation, Quill } from 'quill';

@@ -7,6 +8,2 @@ import * as Y from 'yjs';

type PrelimTextType = 'splitLeft' | 'splitRight';
export type TextType = PrelimText | Text;
// Removes the pending '\n's if it has no attributes

@@ -37,62 +34,2 @@ export function normQuillDelta(delta: DeltaOperation[]): DeltaOperation[] {

const UNSUPPORTED_MSG = 'PrelimText does not support ';
export class PrelimText {
ready = false;
type: PrelimTextType;
index: number;
constructor(type: PrelimTextType, index: number) {
this.type = type;
this.index = index;
}
get length() {
return 0;
}
clone() {
throw new Error(UNSUPPORTED_MSG + 'clone');
}
insert() {
throw new Error(UNSUPPORTED_MSG + 'insert');
}
insertList() {
throw new Error(UNSUPPORTED_MSG + 'insertList');
}
split() {
throw new Error(UNSUPPORTED_MSG + 'split');
}
join() {
throw new Error(UNSUPPORTED_MSG + 'join');
}
clear() {
throw new Error(UNSUPPORTED_MSG + 'clear');
}
delete() {
throw new Error(UNSUPPORTED_MSG + 'delete');
}
replace() {
throw new Error(UNSUPPORTED_MSG + 'replace');
}
format() {
throw new Error(UNSUPPORTED_MSG + 'format');
}
applyDelta() {
throw new Error(UNSUPPORTED_MSG + 'applyDelta');
}
sliceToDelta() {
throw new Error(UNSUPPORTED_MSG + 'sliceToDelta');
}
}
declare module 'yjs' {

@@ -120,6 +57,2 @@ interface Text {

private _yText: Y.Text;
/**
* @internal
*/
public delayedJobs: (() => void)[] = [];

@@ -137,15 +70,6 @@ // TODO toggle transact by options

/**
* @internal
*/
public doDelayedJobs() {
this.delayedJobs.forEach(cb => cb());
this.delayedJobs = [];
}
static fromDelta(delta: DeltaOperation[]) {
const result = new Text('');
// In the first time, yDoc does not exist.
result.delayedJobs.push(() => result.applyDelta(delta));
return result;
const result = new Y.Text();
result.applyDelta(delta);
return new Text(result);
}

@@ -157,2 +81,6 @@

get yText() {
return this._yText;
}
private _transact(callback: () => void) {

@@ -176,7 +104,52 @@ if (this._shouldTransact) {

split(index: number, length: number): [PrelimText, PrelimText] {
return [
new PrelimText('splitLeft', index),
new PrelimText('splitRight', index + length),
];
/**
* Here are three cases for point position(index + length):
* [{insert: 'abc', ...}, {insert: 'def', ...}, {insert: 'ghi', ...}]
* 1. abc|de|fghi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'f', ...}, {insert: 'ghi', ...}]
* 2. abc|def|ghi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'ghi', ...}]
* 3. abc|defg|hi
* left: [{insert: 'abc', ...}]
* right: [{insert: 'hi', ...}]
*/
split(index: number, length: number): Text {
const deltas = this._yText.toDelta();
if (deltas instanceof Array) {
let tmpIndex = 0;
const rightDeltas: DeltaInsert[] = [];
for (let i = 0; i < deltas.length; i++) {
const insert = deltas[i].insert;
if (typeof insert === 'string') {
if (tmpIndex + insert.length >= index + length) {
const insertRight = insert.slice(index + length - tmpIndex);
rightDeltas.push({
insert: insertRight,
attributes: deltas[i].attributes,
});
rightDeltas.push(...deltas.slice(i + 1));
break;
}
tmpIndex += insert.length;
} else {
throw new Error(
'This text cannot be split because it contains non-string insert.'
);
}
}
this.delete(index, this.length - index);
const rightYText = new Y.Text();
rightYText.applyDelta(rightDeltas);
const rightText = new Text(rightYText);
return rightText;
} else {
throw new Error(
'This text cannot be split because we failed to get the deltas of it.'
);
}
}

@@ -183,0 +156,0 @@

@@ -6,3 +6,3 @@ import { isPrimitive, matchFlavours, SYS_KEYS } from '@blocksuite/global/utils';

import type { BaseBlockModel } from '../base.js';
import { PrelimText, Text, TextType } from '../text-adapter.js';
import { Text } from '../text-adapter.js';
import type { Workspace } from '../workspace/index.js';

@@ -57,3 +57,6 @@ import type {

// TODO use schema
if (key === 'text') return;
if (key === 'text' && value instanceof Text) {
yBlock.set(`prop:${key}`, value.yText);
return;
}
if (!isPrimitive(value) && !Array.isArray(value)) {

@@ -84,58 +87,2 @@ throw new Error('Only top level primitives are supported for now');

export function trySyncTextProp(
splitSet: Set<Text | PrelimText>,
yBlock: YBlock,
text?: TextType | void
) {
if (!text) return;
// update by clone
if (text instanceof Text) {
// @ts-ignore
yBlock.set('prop:text', text._yText);
text.doDelayedJobs();
return;
}
// update by split
if (text instanceof PrelimText) {
const iter = splitSet.values();
const base = iter.next().value as Text;
const left = iter.next().value as PrelimText;
const right = iter.next().value as PrelimText;
if (!left.ready) {
throw new Error('PrelimText left is not ready');
}
if (
left.type !== 'splitLeft' ||
right.type !== 'splitRight' ||
right !== text
) {
throw new Error('Unmatched text entity');
}
// @ts-ignore
const yBase = base._yText;
// attach meta state for identifying split
// otherwise local change from y-side will be ignored by TextAdapter
// @ts-ignore
yBase.meta = { split: true };
// clone the original text to `yRight` and add it to the doc first
const yRight = yBase.clone();
yBlock.set('prop:text', yRight);
// delete the left-half part of `yRight`, making it the new right
yRight.delete(0, right.index);
// delete the right-half part of `yBase`, making it the new left
yBase.delete(left.index, yBase.length - left.index);
// cleanup
splitSet.clear();
}
}
export function toBlockProps(yBlock: YBlock): Partial<BlockProps> {

@@ -142,0 +89,0 @@ const prefixedProps = yBlock.toJSON() as PrefixedBlockProps;

@@ -12,8 +12,3 @@ import type { BlockTag, TagSchema } from '@blocksuite/global/database';

import { Space, StackItem } from '../space.js';
import {
PrelimText,
RichTextAdapter,
Text,
TextType,
} from '../text-adapter.js';
import { RichTextAdapter, Text } from '../text-adapter.js';
import type { IdGenerator } from '../utils/id-generator.js';

@@ -25,3 +20,2 @@ import {

toBlockProps,
trySyncTextProp,
} from '../utils/utils.js';

@@ -39,3 +33,3 @@ import type { BlockSuiteDoc } from '../yjs/index.js';

flavour: string;
text?: void | TextType;
text?: Text;
children?: BaseBlockModel[];

@@ -65,3 +59,2 @@ };

private _blockMap = new Map<string, BaseBlockModel>();
private _splitSet = new Set<Text | PrelimText>();
private _synced = false;

@@ -234,4 +227,6 @@

setTagSchema(schema: TagSchema) {
this.transact(() => this.tagSchema.set(schema.id, schema));
setTagSchema(schema: Omit<TagSchema, 'id'>): string {
const id = this._idGenerator();
this.transact(() => this.tagSchema.set(id, { ...schema, id }));
return id;
}

@@ -367,3 +362,2 @@

syncBlockProps(defaultProps, yBlock, clonedProps, this._ignoredKeys);
trySyncTextProp(this._splitSet, yBlock, clonedProps.text);

@@ -451,8 +445,5 @@ if (typeof parent === 'string') {

this.transact(() => {
if (props.text instanceof PrelimText) {
props.text.ready = true;
} else if (props.text instanceof Text) {
if (props.text instanceof Text) {
model.text = props.text;
// @ts-ignore
yBlock.set('prop:text', props.text._yText);
yBlock.set('prop:text', props.text.yText);
}

@@ -588,6 +579,2 @@

markTextSplit(base: Text, left: PrelimText, right: PrelimText) {
this._splitSet.add(base).add(left).add(right);
}
syncFromExistingDoc() {

@@ -594,0 +581,0 @@ if (this._synced) {

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