Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions packages/core/src/api/BlocksAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,40 +68,51 @@ describe('BlocksAPI', () => {
it('should pass explicit index to blocksManager.deleteBlock', () => {
const api = new BlocksAPI(
blocksManager,
{ defaultBlock } as CoreConfigValidated,
{
defaultBlock,
userId: 'userId',
} as CoreConfigValidated,
new EditorJSModel('userId', { identifier: 'docId' })
);

api.delete({ block: 2 });

expect(blocksManager.deleteBlock).toHaveBeenCalledWith(2);
expect(blocksManager.deleteBlock).toHaveBeenCalledWith(2, 'userId');
});

it('should pass undefined when index is omitted', () => {
const api = new BlocksAPI(
blocksManager,
{ defaultBlock } as CoreConfigValidated,
{
defaultBlock,
userId: 'userId',
} as CoreConfigValidated,
new EditorJSModel('userId', { identifier: 'docId' })
);

api.delete();

expect(blocksManager.deleteBlock).toHaveBeenCalledWith(undefined);
expect(blocksManager.deleteBlock).toHaveBeenCalledWith(undefined, 'userId');
});
});

describe('.move()', () => {
it('should call blocksManager.move with toIndex and fromIndex', () => {
const api = new BlocksAPI(
blocksManager,
{ defaultBlock } as CoreConfigValidated,
{
defaultBlock,
userId: 'userId',
} as CoreConfigValidated,
new EditorJSModel('userId', { identifier: 'docId' })
);

api.move({ toIndex: 3,
fromIndex: 1 });
api.move({
toIndex: 3,
fromIndex: 1,
});

expect(blocksManager.move).toHaveBeenCalledWith(3, 1);
expect(blocksManager.move).toHaveBeenCalledWith(3, 1, 'userId');
});
});

Expand All @@ -124,7 +135,10 @@ describe('BlocksAPI', () => {
it('should pass blocks and index to blocksManager.insertMany', () => {
const api = new BlocksAPI(
blocksManager,
{ defaultBlock } as CoreConfigValidated,
{
defaultBlock,
userId: 'userId',
} as CoreConfigValidated,
new EditorJSModel('userId', { identifier: 'docId' })
);

Expand All @@ -138,13 +152,16 @@ describe('BlocksAPI', () => {
index: 4,
});

expect(blocksManager.insertMany).toHaveBeenCalledWith(blocks, 4);
expect(blocksManager.insertMany).toHaveBeenCalledWith(blocks, 4, 'userId');
});

it('should pass undefined index to blocksManager.insertMany when omitted', () => {
const api = new BlocksAPI(
blocksManager,
{ defaultBlock } as CoreConfigValidated,
{
defaultBlock,
userId: 'userId',
} as CoreConfigValidated,
new EditorJSModel('userId', { identifier: 'docId' })
);

Expand All @@ -155,7 +172,7 @@ describe('BlocksAPI', () => {

api.insertMany({ blocks });

expect(blocksManager.insertMany).toHaveBeenCalledWith(blocks, undefined);
expect(blocksManager.insertMany).toHaveBeenCalledWith(blocks, undefined, 'userId');
});
});

Expand Down
34 changes: 21 additions & 13 deletions packages/core/src/api/BlocksAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,21 @@
* Removes Block by index or id, or current block if params are not passed
* @param params - delete parameters
* @param params.block - index or id of a block to delete
* @param params.userId - user id to attribute the change to
*/
public delete({ block }: NonNullable<Parameters<BlocksApiInterface['delete']>[0]> = {}): void {
return this.#blocksManager.deleteBlock(block);
public delete({ block, userId = this.#config.userId }: NonNullable<Parameters<BlocksApiInterface['delete']>[0]> = {}): void {
return this.#blocksManager.deleteBlock(block, userId);
}

/**
* Moves a block to a new index
* @param params - move parameters
* @param params.toIndex - index where the block is moved to
* @param [params.fromIndex] - block to move. Current block if not passed
* @param [params.userId] - user id to attribute the change to
*/
public move({ toIndex, fromIndex }: Parameters<BlocksApiInterface['move']>[0]): void {
return this.#blocksManager.move(toIndex, fromIndex);
public move({ toIndex, fromIndex, userId = this.#config.userId }: Parameters<BlocksApiInterface['move']>[0]): void {
return this.#blocksManager.move(toIndex, fromIndex, userId);
}

/**
Expand All @@ -98,9 +100,10 @@
* @param params - insertMany parameters
* @param params.blocks - array of blocks to insert
* @param [params.index] - index to insert blocks at. If undefined, inserts at the end
* @param [params.userId] - user id to attribute the change to
*/
public insertMany({ blocks, index }: Parameters<BlocksApiInterface['insertMany']>[0]): void {
return this.#blocksManager.insertMany(blocks, index);
public insertMany({ blocks, index, userId = this.#config.userId }: Parameters<BlocksApiInterface['insertMany']>[0]): void {
return this.#blocksManager.insertMany(blocks, index, userId);
}

/**
Expand All @@ -111,8 +114,9 @@
* @param [params.index] - index to insert block at
* @param [params.focus] - flag indicates if new block should be focused @todo implement
* @param [params.replace] - flag indicates if block at index should be replaced @todo implement
* @param [params.userId] - user id to attribute the change to
*/
public insert({ type, data, index, focus, replace }: NonNullable<Parameters<BlocksApiInterface['insert']>[0]> = {}): void {
public insert({ type, data, index, focus, replace, userId = this.#config.userId }: NonNullable<Parameters<BlocksApiInterface['insert']>[0]> = {}): void {
const blockType = type ?? this.#config.defaultBlock;
const blockData = data ?? {};

Expand All @@ -122,6 +126,7 @@
index,
replace,
focus,
userId,
});
};

Expand Down Expand Up @@ -157,14 +162,15 @@
* @param params.block - index or id of the block
* @param params.key - data key of the new data node
* @param [params.initialData] - optional initial data
* @param [params.userId] - user id to attribute the change to
*/
public createData({ block, key, initialData }: Parameters<BlocksApiInterface['createData']>[0]): void {
public createData({ block, key, initialData, userId = this.#config.userId }: Parameters<BlocksApiInterface['createData']>[0]): void {

Check warning on line 167 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 167 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
this.#model.createDataNode(
this.#config.userId,
userId,
block as BlockIndexOrId,
key,
initialData
);

Check warning on line 173 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
Expand All @@ -172,9 +178,10 @@
* @param params - removeData parameters
* @param params.block - index or identifier of the block
* @param params.key - data key of the node to remove
* @param [params.userId] - user id to attribute the change to
*/
public removeData({ block, key }: Parameters<BlocksApiInterface['removeData']>[0]): void {
this.#model.removeDataNode(this.#config.userId, block as BlockIndexOrId, key);
public removeData({ block, key, userId = this.#config.userId }: Parameters<BlocksApiInterface['removeData']>[0]): void {

Check warning on line 183 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 183 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
this.#model.removeDataNode(userId, block as BlockIndexOrId, key);

Check warning on line 184 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

/**
Expand All @@ -183,8 +190,9 @@
* @param params.block - index or identifier of the block
* @param params.key - key of the data node to update
* @param params.value - new value
* @param [params.userId] - user id to attribute the change to
*/
public updateValue({ block, key, value }: Parameters<BlocksApiInterface['updateValue']>[0]): void {
this.#model.updateValue(this.#config.userId, block as BlockIndexOrId, createDataKey(key), value);
public updateValue({ block, key, value, userId = this.#config.userId }: Parameters<BlocksApiInterface['updateValue']>[0]): void {

Check warning on line 195 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 195 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
this.#model.updateValue(userId, block as BlockIndexOrId, createDataKey(key), value);

Check warning on line 196 in packages/core/src/api/BlocksAPI.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
}
20 changes: 12 additions & 8 deletions packages/core/src/api/TextAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ export class TextAPI implements TextAPIInterface {
* @param params.block - block index or identifier
* @param params.key - data key of the text node
* @param params.start - start offset
* @param [params.userId] - user id to attribute the change to
*/
public insert({ text, block, key, start }: Parameters<TextAPIInterface['insert']>[0]): void {
public insert({ text, block, key, start, userId = this.#config.userId }: Parameters<TextAPIInterface['insert']>[0]): void {
this.#model.insertText(
this.#config.userId,
userId,
block as BlockIndexOrId,
createDataKey(key),
text,
Expand All @@ -63,10 +64,11 @@ export class TextAPI implements TextAPIInterface {
* @param params.key - data key of the text node
* @param params.start - range start
* @param params.end - range end
* @param [params.userId] - user id to attribute the change to
*/
public remove({ block, key, start, end }: Parameters<TextAPIInterface['remove']>[0]): string {
public remove({ block, key, start, end, userId = this.#config.userId }: Parameters<TextAPIInterface['remove']>[0]): string {
return this.#model.removeText(
this.#config.userId,
userId,
block as BlockIndexOrId,
createDataKey(key),
start,
Expand All @@ -83,10 +85,11 @@ export class TextAPI implements TextAPIInterface {
* @param params.start - range start
* @param params.end - range end
* @param params.data - optional tool's data
* @param [params.userId] - user id to attribute the change to
*/
public format({ tool, block, key, start, end, data }: Parameters<TextAPIInterface['format']>[0]): void {
public format({ tool, block, key, start, end, data, userId = this.#config.userId }: Parameters<TextAPIInterface['format']>[0]): void {
this.#model.format(
this.#config.userId,
userId,
block as BlockIndexOrId,
createDataKey(key),
createInlineToolName(tool),
Expand All @@ -104,10 +107,11 @@ export class TextAPI implements TextAPIInterface {
* @param params.key - data key of the text node
* @param params.start - range start
* @param params.end - range end
* @param [params.userId] - user id to attribute the change to
*/
public unformat({ tool, block, key, start, end }: Parameters<TextAPIInterface['unformat']>[0]): void {
public unformat({ tool, block, key, start, end, userId = this.#config.userId }: Parameters<TextAPIInterface['unformat']>[0]): void {
this.#model.unformat(
this.#config.userId,
userId,
block as BlockIndexOrId,
createDataKey(key),
createInlineToolName(tool),
Expand Down
27 changes: 18 additions & 9 deletions packages/core/src/components/BlockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ interface InsertBlockParameters {
*/
focus?: boolean;
// tunes?: {[name: string]: BlockTuneData};
/**
* User id to attribute the change to
*/
userId?: string | number;
}

/**
Expand Down Expand Up @@ -113,6 +117,7 @@ export class BlocksManager {
* @param parameters.index - index to insert block at
// * @param parameters.needToFocus - flag indicates if caret should be set to block after insert
* @param parameters.replace - flag indicates if block at index should be replaced
* @param parameters.userId - user id to attribute the change to
*/
public insert({
id = undefined,
Expand All @@ -121,6 +126,7 @@ export class BlocksManager {
index,
focus = false,
replace = false,
userId = this.#config.userId,
// tunes = {},
}: InsertBlockParameters = {}): void {
let newIndex = index;
Expand All @@ -130,10 +136,10 @@ export class BlocksManager {
}

if (replace) {
this.#model.removeBlock(this.#config.userId, newIndex);
this.#model.removeBlock(userId, newIndex);
}

this.#model.addBlock(this.#config.userId, {
this.#model.addBlock(userId, {
...data,
id,
name: type,
Expand All @@ -150,9 +156,10 @@ export class BlocksManager {
* Inserts several Blocks to specified index
* @param blocks - array of blocks to insert
* @param [index] - index to insert blocks at. If undefined, inserts at the end
* @param [userId] - user id to attribute the change to
*/
public insertMany(blocks: BlockNodeInit[], index: number = this.#model.length): void {
blocks.forEach((block, i) => this.#model.addBlock(this.#config.userId, block, index + i));
public insertMany(blocks: BlockNodeInit[], index: number = this.#model.length, userId: string | number = this.#config.userId): void {
blocks.forEach((block, i) => this.#model.addBlock(userId, block, index + i));
}

/**
Expand All @@ -173,24 +180,26 @@ export class BlocksManager {
/**
* Removes Block by index, or current block if index is not passed
* @param indexOrId - index or identifier of a block to delete
* @param [userId] - user id to attribute the change to
*/
public deleteBlock(indexOrId: number | string | undefined = this.#getCurrentBlockIndex()): void {
public deleteBlock(indexOrId: number | string | undefined = this.#getCurrentBlockIndex(), userId: string | number = this.#config.userId): void {
if (indexOrId === undefined) {
/**
* @todo see what happens in legacy
*/
throw new Error('No block selected to delete');
}

this.#model.removeBlock(this.#config.userId, indexOrId as BlockIndexOrId);
this.#model.removeBlock(userId, indexOrId as BlockIndexOrId);
}

/**
* Moves a block to a new index
* @param toIndex - index where the block is moved to
* @param [fromIndex] - block to move. Current block if not passed
* @param [userId] - user id to attribute the change to
*/
public move(toIndex: number, fromIndex: number | undefined = this.#getCurrentBlockIndex()): void {
public move(toIndex: number, fromIndex: number | undefined = this.#getCurrentBlockIndex(), userId: string | number = this.#config.userId): void {
if (fromIndex === undefined) {
throw new Error('No block selected to move');
}
Expand All @@ -204,8 +213,8 @@ export class BlocksManager {

const block = this.#model.getBlockSerialized(fromIndex);

this.#model.removeBlock(this.#config.userId, fromIndex);
this.#model.addBlock(this.#config.userId, block, toIndex);
this.#model.removeBlock(userId, fromIndex);
this.#model.addBlock(userId, block, toIndex);
}

/**
Expand Down
Loading
Loading