Skip to content

Commit fd54f16

Browse files
ilyamore88Copilot
andauthored
feat(core): implement Document API (#130)
* feat(sdk): create interface for Document API * feat(core): create DocumentAPI, expose document api via EditorAPI, implement get data method that returns serialized model object * refactor(core): create separate folder for DocumentAPI * test(document-api): add unit test for data getter * chore: apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * chore: add jsdoc --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 350446b commit fd54f16

7 files changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
import { beforeEach, describe, expect, jest } from '@jest/globals';
3+
4+
jest.unstable_mockModule('@editorjs/model', () => {
5+
const EditorJSModel = jest.fn(() => ({
6+
serialized: { blocks: [] },
7+
}));
8+
9+
return {
10+
EditorJSModel,
11+
};
12+
});
13+
14+
const { EditorJSModel } = await import('@editorjs/model');
15+
const { DocumentAPI } = await import('./DocumentAPI.js');
16+
17+
describe('DocumentAPI', () => {
18+
// @ts-expect-error - mock object, don't need to pass any arguments
19+
const model = new EditorJSModel();
20+
21+
const documentAPI = new DocumentAPI(model);
22+
23+
beforeEach(() => {
24+
jest.resetAllMocks();
25+
});
26+
27+
describe('.data', () => {
28+
it('should return serialized model', () => {
29+
const mockedSerializedModel = {
30+
blocks: [
31+
{
32+
name: 'a',
33+
},
34+
{
35+
name: 'b',
36+
},
37+
{
38+
name: 'c',
39+
},
40+
],
41+
};
42+
43+
// @ts-expect-error - need to assign read only property to mock it
44+
model.serialized = mockedSerializedModel;
45+
46+
const data = documentAPI.data;
47+
48+
expect(data).toEqual(mockedSerializedModel);
49+
});
50+
});
51+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'reflect-metadata';
2+
import { Service } from 'typedi';
3+
4+
import { type EditorDocumentSerialized, EditorJSModel } from '@editorjs/model';
5+
import { DocumentAPI as DocumentApiInterface } from '@editorjs/sdk';
6+
7+
/**
8+
* Document API
9+
* - provides access to document serialized data
10+
*/
11+
@Service()
12+
export class DocumentAPI implements DocumentApiInterface {
13+
/**
14+
* Editor document model instance
15+
*/
16+
#model: EditorJSModel;
17+
18+
/**
19+
* DocumentAPI constructor
20+
* All parameters are injected through the IoC container
21+
* @param model - Editor's Document Model instance
22+
*/
23+
constructor(model: EditorJSModel) {
24+
this.#model = model;
25+
}
26+
27+
/**
28+
* Returns serialized document object
29+
*/
30+
public get data(): EditorDocumentSerialized {
31+
return this.#model.serialized;
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './DocumentAPI.js';

packages/core/src/api/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Inject, Service } from 'typedi';
33
import { EditorAPI as EditorApiInterface } from '@editorjs/sdk';
44
import { BlocksAPI } from './BlocksAPI.js';
55
import { SelectionAPI } from './SelectionAPI.js';
6+
import { DocumentAPI } from './DocumentAPI/index.js';
67

78
/**
89
* Class gathers all Editor's APIs
@@ -20,4 +21,10 @@ export class EditorAPI implements EditorApiInterface {
2021
*/
2122
@Inject()
2223
public selection!: SelectionAPI;
24+
25+
/**
26+
* Document API instance to work with document
27+
*/
28+
@Inject()
29+
public document!: DocumentAPI;
2330
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { EditorDocumentSerialized } from '@editorjs/model';
2+
3+
/**
4+
* Document API interface
5+
* Provides methods to work with Editor's document object
6+
*/
7+
export interface DocumentAPI {
8+
/**
9+
* Returns serialized document object
10+
*/
11+
get data(): EditorDocumentSerialized;
12+
}

packages/sdk/src/api/EditorAPI.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BlocksAPI } from './BlocksAPI.js';
22
import type { SelectionAPI } from './SelectionAPI.js';
3+
import type { DocumentAPI } from './DocumentAPI.js';
34

45
/**
56
* Editor API interface
@@ -15,4 +16,9 @@ export interface EditorAPI {
1516
* Selection API instance to work with selection and inline formatting
1617
*/
1718
selection: SelectionAPI;
19+
20+
/**
21+
* Document API instance to work with Editor's document
22+
*/
23+
document: DocumentAPI;
1824
}

packages/sdk/src/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export type * from './EditorAPI.js';
22
export type * from './BlocksAPI.js';
33
export type * from './SelectionAPI.js';
4+
export type * from './DocumentAPI.js';

0 commit comments

Comments
 (0)