-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSelectionAPI.ts
More file actions
80 lines (71 loc) · 2.63 KB
/
Copy pathSelectionAPI.ts
File metadata and controls
80 lines (71 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import 'reflect-metadata';
import { inject, injectable } from 'inversify';
import { SelectionManager } from '../components/SelectionManager.js';
import { Caret, CaretManagerEvents, createInlineToolName, EditorJSModel, EventType, BlockNodeSerialized } from '@editorjs/model';
import { CoreConfigValidated } from '@editorjs/sdk';
import { SelectionAPI as SelectionApiInterface } from '@editorjs/sdk';
import { TOKENS } from '../tokens.js';
/**
* Selection API class
* - provides methods to work with selection
*/
@injectable()
export class SelectionAPI implements SelectionApiInterface {
#selectionManager: SelectionManager;
#model: EditorJSModel;
#config: CoreConfigValidated;
/**
* SelectionAPI class constructor
* @param selectionManager - SelectionManager instance to work with selection and inline formatting
* @param model - EditorJS model instance
* @param config - EditorJS validated config
*/
constructor(
selectionManager: SelectionManager,
model: EditorJSModel,
@inject(TOKENS.EditorConfig) config: CoreConfigValidated
) {
this.#selectionManager = selectionManager;
this.#model = model;
this.#config = config;
}
/**
* Applies passed inline tool to the current selection
* @param params - methods parameters
* @param params.tool - Inline Tool name from the config to apply on the current selection
* @param [params.data] - Inline Tool data to apply to the current selection (e.g. link data)
*/
public applyInlineTool({ tool, data }: Parameters<SelectionApiInterface['applyInlineTool']>[0]): void {
this.#selectionManager.applyInlineToolForCurrentSelection(createInlineToolName(tool), data);
}
/**
* Registers a callback for CaretManager updates. Returns a cleanup function
* @param callback - callback for CaretManager updates
*/
public onCaretUpdate(callback: (event: CaretManagerEvents) => void): () => void {
this.#model.addEventListener(EventType.CaretManagerUpdated, callback);
return () => {
this.#model.removeEventListener(EventType.CaretManagerUpdated, callback);
};
}
/**
* Creates a new caret for a user
* @param userId - user id. If not provided, creates for current user
*/
public createCaret(userId = this.#config.userId): Caret {
return this.#model.createCaret(userId);
}
/**
* Returns user caret
* @param userId - user id. If not provided, returns for current user
*/
public getCaret(userId = this.#config.userId): Caret | undefined {
return this.#model.getCaret(userId);
}
/**
*
*/
public get selectedBlocks(): BlockNodeSerialized[] | null {
return this.#selectionManager.selectedBlocks();
}
}