2022-12-29 23:33:04 +00:00
|
|
|
import {
|
|
|
|
ref,
|
|
|
|
Ref,
|
|
|
|
inject,
|
|
|
|
reactive,
|
|
|
|
} from 'vue';
|
2023-01-02 18:45:49 +00:00
|
|
|
import { IFormattingToolLibrary } from '../types';
|
2022-12-29 23:33:04 +00:00
|
|
|
|
2023-01-02 18:45:49 +00:00
|
|
|
export const SymFormattingToolbarActiveClient = Symbol('Schlechtenburg inline active client');
|
|
|
|
export const SymFormattingToolbarClients = Symbol('Schlechtenburg inline toolbar client elements');
|
|
|
|
export const SymFormattingToolLibrary = Symbol('Schlechtenburg inline tool library');
|
2022-12-29 23:33:04 +00:00
|
|
|
|
2023-01-02 18:45:49 +00:00
|
|
|
export const useFormattingToolbar = () => {
|
|
|
|
const clients: Ref<HTMLElement[]> = inject(SymFormattingToolbarClients, ref([]));
|
|
|
|
const activeClient: Ref<HTMLElement|null> = inject(SymFormattingToolbarActiveClient, ref(null));
|
|
|
|
|
|
|
|
const setActiveClient = (client: HTMLElement|null) => {
|
|
|
|
activeClient.value = client;
|
|
|
|
};
|
2022-12-29 23:33:04 +00:00
|
|
|
|
|
|
|
const setClients = (newClients: HTMLElement[]) => {
|
|
|
|
clients.value = newClients;
|
|
|
|
};
|
|
|
|
|
|
|
|
const clientIsRegistered = (el: HTMLElement) => !!clients.value.find(cl => cl === el);
|
|
|
|
|
|
|
|
const registerClient = (el: HTMLElement) => {
|
|
|
|
if (clientIsRegistered(el)) {
|
|
|
|
console.warn('Not reregistering toolbar client that is already registered:', el);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setClients([
|
|
|
|
...clients.value,
|
|
|
|
el,
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const unregisterClient = (el: HTMLElement) => {
|
|
|
|
setClients(clients.value.filter(cl => cl !== el));
|
|
|
|
};
|
|
|
|
|
2023-01-02 18:45:49 +00:00
|
|
|
const tools: IFormattingToolLibrary = inject(SymFormattingToolLibrary, reactive({}));
|
|
|
|
const getTool = (name: string) => tools[name];
|
|
|
|
const getAllTools = () => Object.keys(tools).map(name => tools[name]);
|
2022-12-29 23:33:04 +00:00
|
|
|
|
2023-01-02 18:45:49 +00:00
|
|
|
return {
|
2022-12-29 23:33:04 +00:00
|
|
|
clients,
|
2023-01-02 18:45:49 +00:00
|
|
|
activeClient,
|
|
|
|
setActiveClient,
|
2022-12-29 23:33:04 +00:00
|
|
|
registerClient,
|
|
|
|
unregisterClient,
|
2023-01-02 18:45:49 +00:00
|
|
|
|
|
|
|
tools,
|
|
|
|
getTool,
|
|
|
|
getAllTools,
|
2022-12-29 23:33:04 +00:00
|
|
|
};
|
|
|
|
};
|