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
51 changes: 51 additions & 0 deletions packages/core/src/api/DocumentAPI/DocumentAPI.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { beforeEach, describe, expect, jest } from '@jest/globals';

jest.unstable_mockModule('@editorjs/model', () => {
const EditorJSModel = jest.fn(() => ({
serialized: { blocks: [] },
}));

return {
EditorJSModel,
};
});

const { EditorJSModel } = await import('@editorjs/model');
const { DocumentAPI } = await import('./DocumentAPI.js');

describe('DocumentAPI', () => {
// @ts-expect-error - mock object, don't need to pass any arguments
const model = new EditorJSModel();

const documentAPI = new DocumentAPI(model);

beforeEach(() => {
jest.resetAllMocks();
Comment thread
ilyamore88 marked this conversation as resolved.
});

describe('.data', () => {
it('should return serialized model', () => {
const mockedSerializedModel = {
blocks: [
{
name: 'a',
},
{
name: 'b',
},
{
name: 'c',
},
],
};

// @ts-expect-error - need to assign read only property to mock it
model.serialized = mockedSerializedModel;

const data = documentAPI.data;

expect(data).toEqual(mockedSerializedModel);
});
});
});
30 changes: 30 additions & 0 deletions packages/core/src/api/DocumentAPI/DocumentAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'reflect-metadata';
import { Service } from 'typedi';

import { type EditorDocumentSerialized, EditorJSModel } from '@editorjs/model';
import { DocumentAPI as DocumentApiInterface } from '@editorjs/sdk';

/**
* Document API
* - provides access to document serialized data
*/
@Service()
export class DocumentAPI implements DocumentApiInterface {
#model: EditorJSModel;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsdoc missing, you could use @link to definition of the model maybe


/**
* DocumentAPI constructor
* All parameters are injected through the IoC container
* @param model - Editor's Document Model instance
*/
constructor(model: EditorJSModel) {
this.#model = model;
}

/**
* Returns serialized document object
*/
public get data(): EditorDocumentSerialized {
return this.#model.serialized;
}
}
1 change: 1 addition & 0 deletions packages/core/src/api/DocumentAPI/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DocumentAPI.js';
7 changes: 7 additions & 0 deletions packages/core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Inject, Service } from 'typedi';
import { EditorAPI as EditorApiInterface } from '@editorjs/sdk';
import { BlocksAPI } from './BlocksAPI.js';
import { SelectionAPI } from './SelectionAPI.js';
import { DocumentAPI } from './DocumentAPI/index.js';

/**
* Class gathers all Editor's APIs
Expand All @@ -20,4 +21,10 @@ export class EditorAPI implements EditorApiInterface {
*/
@Inject()
public selection!: SelectionAPI;

/**
* Document API instance to work with document
*/
@Inject()
public document!: DocumentAPI;
}
12 changes: 12 additions & 0 deletions packages/sdk/src/api/DocumentAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { EditorDocumentSerialized } from '@editorjs/model';

/**
* Document API interface
* Provides methods to work with Editor's document object
*/
export interface DocumentAPI {
/**
* Returns serialized document object
*/
get data(): EditorDocumentSerialized;
}
6 changes: 6 additions & 0 deletions packages/sdk/src/api/EditorAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BlocksAPI } from './BlocksAPI.js';
import type { SelectionAPI } from './SelectionAPI.js';
import type { DocumentAPI } from './DocumentAPI.js';

/**
* Editor API interface
Expand All @@ -15,4 +16,9 @@ export interface EditorAPI {
* Selection API instance to work with selection and inline formatting
*/
selection: SelectionAPI;

/**
* Document API instance to work with Editor's document
*/
document: DocumentAPI;
}
1 change: 1 addition & 0 deletions packages/sdk/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export type * from './EditorAPI.js';
export type * from './BlocksAPI.js';
export type * from './SelectionAPI.js';
export type * from './DocumentAPI.js';
Loading