|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { logger } from '../../utils/logger.ts' |
| 7 | + |
| 8 | +window._vue_richtext_custom_picker_elements ??= {} |
| 9 | +window._registerCustomPickerElement ??= registerCustomPickerElement |
| 10 | + |
| 11 | +/** |
| 12 | + * Representation of the render callback result |
| 13 | + * It contains a dom element and an object (Vue instance or other fancy things) |
| 14 | + */ |
| 15 | +export class NcCustomPickerRenderResult { |
| 16 | + |
| 17 | + public element: HTMLElement |
| 18 | + public object: object |
| 19 | + |
| 20 | + /** |
| 21 | + * @param element - The HTML element |
| 22 | + * @param object - The object |
| 23 | + */ |
| 24 | + constructor(element: HTMLElement, object: object) { |
| 25 | + this.element = element |
| 26 | + this.object = object |
| 27 | + } |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +interface CustomPickerElementProps { |
| 32 | + providerId: number |
| 33 | + accessible: boolean |
| 34 | +} |
| 35 | + |
| 36 | +type CustomPickerElementRegistrationCallback = (el: HTMLElement, options: CustomPickerElementProps) => void |
| 37 | +type CustomPickerElementDestroyCallback = (el: HTMLElement, result: NcCustomPickerRenderResult) => void |
| 38 | + |
| 39 | +export interface CustomPickerElement { |
| 40 | + id: string |
| 41 | + size: 'small' | 'normal' | 'large' | 'full' |
| 42 | + |
| 43 | + callback: CustomPickerElementRegistrationCallback |
| 44 | + onDestroy: CustomPickerElementDestroyCallback |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * @param id - Id of the element to check |
| 49 | + */ |
| 50 | +export function isCustomPickerElementRegistered(id: string): boolean { |
| 51 | + return !!window._vue_richtext_custom_picker_elements[id] |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @param id - Id of the element to get |
| 56 | + */ |
| 57 | +export function getCustomPickerElementSize(id: string): 'small' | 'normal' | 'large' | 'full' | null { |
| 58 | + const size = window._vue_richtext_custom_picker_elements[id]?.size |
| 59 | + if (['small', 'normal', 'large', 'full'].includes(size)) { |
| 60 | + return size |
| 61 | + } |
| 62 | + return null |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * @param id - Id of element to register |
| 67 | + * @param callback - Render callback |
| 68 | + * @param onDestroy - Cleanup callback |
| 69 | + * @param size - Size of the element |
| 70 | + */ |
| 71 | +export function registerCustomPickerElement( |
| 72 | + id: string, |
| 73 | + callback: CustomPickerElementRegistrationCallback, |
| 74 | + onDestroy: CustomPickerElementDestroyCallback = () => {}, |
| 75 | + size: CustomPickerElement['size'] = 'large', |
| 76 | +) { |
| 77 | + if (window._vue_richtext_custom_picker_elements[id]) { |
| 78 | + logger.error(`Custom reference picker element for id ${id} already registered`) |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + window._vue_richtext_custom_picker_elements[id] = { |
| 83 | + id, |
| 84 | + callback, |
| 85 | + onDestroy, |
| 86 | + size, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * @param el - element to render to |
| 92 | + * @param options - Element options |
| 93 | + */ |
| 94 | +export function renderCustomPickerElement(el: HTMLElement, options: CustomPickerElementProps) { |
| 95 | + const { providerId, accessible } = options |
| 96 | + if (!window._vue_richtext_custom_picker_elements[providerId]) { |
| 97 | + logger.error(`Custom reference picker element for reference provider ID ${providerId} not registered`) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + return window._vue_richtext_custom_picker_elements[providerId] |
| 102 | + .callback(el, { providerId, accessible }) |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * @param providerId - Provider id of element to destroy |
| 107 | + * @param el - The element to destroy element from |
| 108 | + * @param renderResult - The render result |
| 109 | + */ |
| 110 | +export function destroyCustomPickerElement(providerId: string, el: HTMLElement, renderResult: NcCustomPickerRenderResult) { |
| 111 | + if (!window._vue_richtext_custom_picker_elements[providerId]) { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + window._vue_richtext_custom_picker_elements[providerId] |
| 116 | + .onDestroy(el, renderResult) |
| 117 | +} |
0 commit comments