@blocksuite/store
Advanced tools
Comparing version
@@ -0,1 +1,2 @@ | ||
import type { TextAttributes } from '@blocksuite/virgo'; | ||
import type { DeltaOperation, Quill } from 'quill'; | ||
@@ -40,2 +41,4 @@ import * as Y from 'yjs'; | ||
/** | ||
* NOTE: The string included in [index, index + length) will be deleted. | ||
* | ||
* Here are three cases for point position(index + length): | ||
@@ -53,4 +56,7 @@ * [{insert: 'abc', ...}, {insert: 'def', ...}, {insert: 'ghi', ...}] | ||
*/ | ||
split(index: number, length: number): Text; | ||
split(index: number, length?: number): Text; | ||
insert(content: string, index: number, attributes?: Record<string, unknown>): void; | ||
/** | ||
* @deprecated Use {@link insert} or {@link applyDelta} instead. | ||
*/ | ||
insertList(insertTexts: DeltaOperation[], index: number): void; | ||
@@ -60,3 +66,3 @@ join(other: Text): void; | ||
delete(index: number, length: number): void; | ||
replace(index: number, length: number, content: string, attributes?: Record<string, unknown>): void; | ||
replace(index: number, length: number, content: string, attributes?: TextAttributes): void; | ||
clear(): void; | ||
@@ -63,0 +69,0 @@ applyDelta(delta: DeltaOperation[]): void; |
@@ -64,2 +64,4 @@ import * as Y from 'yjs'; | ||
/** | ||
* NOTE: The string included in [index, index + length) will be deleted. | ||
* | ||
* Here are three cases for point position(index + length): | ||
@@ -77,36 +79,53 @@ * [{insert: 'abc', ...}, {insert: 'def', ...}, {insert: 'ghi', ...}] | ||
*/ | ||
split(index, length) { | ||
split(index, length = 0) { | ||
if (index < 0 || length < 0 || index + length > this._yText.length) { | ||
throw new Error('Failed to split text! Index or length out of range, index: ' + | ||
index + | ||
', length: ' + | ||
length + | ||
', text length: ' + | ||
this._yText.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; | ||
if (!(deltas instanceof Array)) { | ||
throw new Error('This text cannot be split because we failed to get the deltas of it.'); | ||
} | ||
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; | ||
} | ||
else { | ||
throw new Error('This text cannot be split because it contains non-string insert.'); | ||
} | ||
tmpIndex += insert.length; | ||
} | ||
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 it contains non-string insert.'); | ||
} | ||
} | ||
else { | ||
throw new Error('This text cannot be split because we failed to get the deltas of it.'); | ||
} | ||
this.delete(index, this.length - index); | ||
const rightYText = new Y.Text(); | ||
rightYText.applyDelta(rightDeltas); | ||
const rightText = new Text(rightYText); | ||
return rightText; | ||
} | ||
insert(content, index, attributes) { | ||
if (!content.length) { | ||
return; | ||
} | ||
if (index < 0 || index > this._yText.length) { | ||
throw new Error('Failed to insert text! Index or length out of range, index: ' + | ||
index + | ||
', length: ' + | ||
length + | ||
', text length: ' + | ||
this._yText.length); | ||
} | ||
this._transact(() => { | ||
@@ -117,2 +136,5 @@ this._yText.insert(index, content, attributes); | ||
} | ||
/** | ||
* @deprecated Use {@link insert} or {@link applyDelta} instead. | ||
*/ | ||
insertList(insertTexts, index) { | ||
@@ -138,2 +160,13 @@ this._transact(() => { | ||
format(index, length, format) { | ||
if (length === 0) { | ||
return; | ||
} | ||
if (index < 0 || length < 0 || index + length > this._yText.length) { | ||
throw new Error('Failed to format text! Index or length out of range, index: ' + | ||
index + | ||
', length: ' + | ||
length + | ||
', text length: ' + | ||
this._yText.length); | ||
} | ||
this._transact(() => { | ||
@@ -145,2 +178,13 @@ this._yText.format(index, length, format); | ||
delete(index, length) { | ||
if (length === 0) { | ||
return; | ||
} | ||
if (index < 0 || length < 0 || index + length > this._yText.length) { | ||
throw new Error('Failed to delete text! Index or length out of range, index: ' + | ||
index + | ||
', length: ' + | ||
length + | ||
', text length: ' + | ||
this._yText.length); | ||
} | ||
this._transact(() => { | ||
@@ -152,2 +196,11 @@ this._yText.delete(index, length); | ||
replace(index, length, content, attributes) { | ||
if (index < 0 || length < 0 || index + length > this._yText.length) { | ||
throw new Error('Failed to replace text! The length of the text is' + | ||
this._yText.length + | ||
', but you are trying to replace from' + | ||
index + | ||
'to' + | ||
index + | ||
length); | ||
} | ||
this._transact(() => { | ||
@@ -154,0 +207,0 @@ this._yText.delete(index, length); |
{ | ||
"name": "@blocksuite/store", | ||
"version": "0.4.0-20230214234726-0f75778", | ||
"version": "0.4.0-20230215160945-c645720", | ||
"description": "BlockSuite data store built for general purpose state management.", | ||
@@ -11,4 +11,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@blocksuite/global": "0.4.0-20230214234726-0f75778", | ||
"@blocksuite/virgo": "0.4.0-20230214234726-0f75778", | ||
"@blocksuite/global": "0.4.0-20230215160945-c645720", | ||
"@blocksuite/virgo": "0.4.0-20230215160945-c645720", | ||
"@types/flexsearch": "^0.7.3", | ||
@@ -24,7 +24,7 @@ "buffer": "^6.0.3", | ||
"y-webrtc": "^10.2.4", | ||
"zod": "^3.20.2" | ||
"zod": "^3.20.6" | ||
}, | ||
"devDependencies": { | ||
"lit": "^2.6.1", | ||
"yjs": "^13.5.45" | ||
"yjs": "^13.5.46" | ||
}, | ||
@@ -31,0 +31,0 @@ "peerDependencies": { |
@@ -0,1 +1,2 @@ | ||
import { getDefaultPlaygroundURL } from '@blocksuite/global/utils'; | ||
import { test } from '@playwright/test'; | ||
@@ -6,3 +7,6 @@ | ||
// checkout test-entry.ts for actual test cases | ||
const blobExamplePage = 'http://localhost:5173/examples/blob/'; | ||
const blobExamplePage = new URL( | ||
'/examples/blob', | ||
getDefaultPlaygroundURL(!!process.env.CI) | ||
).toString(); | ||
@@ -9,0 +13,0 @@ test('blob storage basics', async ({ page }) => { |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { DeltaInsert } from '@blocksuite/virgo'; | ||
import type { DeltaInsert, TextAttributes } from '@blocksuite/virgo'; | ||
import type { DeltaOperation, Quill } from 'quill'; | ||
@@ -100,2 +100,4 @@ import * as Y from 'yjs'; | ||
/** | ||
* NOTE: The string included in [index, index + length) will be deleted. | ||
* | ||
* Here are three cases for point position(index + length): | ||
@@ -113,42 +115,63 @@ * [{insert: 'abc', ...}, {insert: 'def', ...}, {insert: 'ghi', ...}] | ||
*/ | ||
split(index: number, length: number): Text { | ||
split(index: number, length = 0): Text { | ||
if (index < 0 || length < 0 || index + length > this._yText.length) { | ||
throw new Error( | ||
'Failed to split text! Index or length out of range, index: ' + | ||
index + | ||
', length: ' + | ||
length + | ||
', text length: ' + | ||
this._yText.length | ||
); | ||
} | ||
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.' | ||
); | ||
if (!(deltas instanceof Array)) { | ||
throw new Error( | ||
'This text cannot be split because we failed to get the deltas of it.' | ||
); | ||
} | ||
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); | ||
this.delete(index, this.length - index); | ||
const rightYText = new Y.Text(); | ||
rightYText.applyDelta(rightDeltas); | ||
const rightText = new Text(rightYText); | ||
return rightText; | ||
} else { | ||
return rightText; | ||
} | ||
insert(content: string, index: number, attributes?: Record<string, unknown>) { | ||
if (!content.length) { | ||
return; | ||
} | ||
if (index < 0 || index > this._yText.length) { | ||
throw new Error( | ||
'This text cannot be split because we failed to get the deltas of it.' | ||
'Failed to insert text! Index or length out of range, index: ' + | ||
index + | ||
', length: ' + | ||
length + | ||
', text length: ' + | ||
this._yText.length | ||
); | ||
} | ||
} | ||
insert(content: string, index: number, attributes?: Record<string, unknown>) { | ||
this._transact(() => { | ||
@@ -160,2 +183,5 @@ this._yText.insert(index, content, attributes); | ||
/** | ||
* @deprecated Use {@link insert} or {@link applyDelta} instead. | ||
*/ | ||
insertList(insertTexts: DeltaOperation[], index: number) { | ||
@@ -186,2 +212,15 @@ this._transact(() => { | ||
format(index: number, length: number, format: any) { | ||
if (length === 0) { | ||
return; | ||
} | ||
if (index < 0 || length < 0 || index + length > this._yText.length) { | ||
throw new Error( | ||
'Failed to format text! Index or length out of range, index: ' + | ||
index + | ||
', length: ' + | ||
length + | ||
', text length: ' + | ||
this._yText.length | ||
); | ||
} | ||
this._transact(() => { | ||
@@ -194,2 +233,15 @@ this._yText.format(index, length, format); | ||
delete(index: number, length: number) { | ||
if (length === 0) { | ||
return; | ||
} | ||
if (index < 0 || length < 0 || index + length > this._yText.length) { | ||
throw new Error( | ||
'Failed to delete text! Index or length out of range, index: ' + | ||
index + | ||
', length: ' + | ||
length + | ||
', text length: ' + | ||
this._yText.length | ||
); | ||
} | ||
this._transact(() => { | ||
@@ -205,4 +257,15 @@ this._yText.delete(index, length); | ||
content: string, | ||
attributes?: Record<string, unknown> | ||
attributes?: TextAttributes | ||
) { | ||
if (index < 0 || length < 0 || index + length > this._yText.length) { | ||
throw new Error( | ||
'Failed to replace text! The length of the text is' + | ||
this._yText.length + | ||
', but you are trying to replace from' + | ||
index + | ||
'to' + | ||
index + | ||
length | ||
); | ||
} | ||
this._transact(() => { | ||
@@ -209,0 +272,0 @@ this._yText.delete(index, length); |
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
513589
0.97%8418
1.53%+ Added
+ Added
- Removed
- Removed
Updated