UNPKG

@nextcloud/vue

Version:
1 lines 9.23 kB
{"version":3,"file":"customPickerElements-GyIIOHiQ.cjs","sources":["../../src/functions/reference/widgets.ts","../../src/functions/reference/customPickerElements.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport { logger } from '../../utils/logger.ts'\n\nexport interface ReferenceWidgetRenderProperties {\n\trichObjectType: string\n\trichObject: object\n\taccessible: boolean\n\tinteractive: boolean\n}\n\ntype ReferenceWidgetRenderCallback = (el: HTMLElement, properties: ReferenceWidgetRenderProperties) => void\ntype ReferenceWidgetDestroyCallback = (el: HTMLElement) => void\n\nexport interface ReferenceWidgetProps {\n\tid: string\n\thasInteractiveView: boolean\n\tfullWidth: boolean\n\tcallback: ReferenceWidgetRenderCallback\n\tonDestroy: ReferenceWidgetDestroyCallback\n}\n\nwindow._vue_richtext_widgets ??= {}\nwindow._registerWidget ??= (id: string, callback: ReferenceWidgetRenderCallback, onDestroy: ReferenceWidgetDestroyCallback, props: Partial<ReferenceWidgetProps>) => {\n\tregisterWidget(id, callback, onDestroy, props)\n}\n\n/**\n * Register a new reference widget\n *\n * @param id - Id tof the widget\n * @param callback - Render callback\n * @param onDestroy - Cleanup callback\n * @param props - Widget props\n */\nexport function registerWidget(\n\tid: string,\n\tcallback: ReferenceWidgetRenderCallback,\n\tonDestroy: ReferenceWidgetDestroyCallback = () => {},\n\tprops?: Partial<ReferenceWidgetProps>,\n) {\n\tconst propsWithDefaults = {\n\t\thasInteractiveView: true,\n\t\tfullWidth: false,\n\t\t...props,\n\t}\n\n\tif (window._vue_richtext_widgets[id]) {\n\t\tlogger.error(`[ReferencePicker]: Widget for id ${id} already registered`)\n\t\treturn\n\t}\n\n\twindow._vue_richtext_widgets[id] = {\n\t\tid,\n\t\tcallback,\n\t\tonDestroy,\n\t\t...propsWithDefaults,\n\t}\n}\n\n/**\n * Render a reference widget to a given HTML element.\n *\n * @param el - The element to render widget to\n * @param options - render options\n */\nexport function renderWidget(el: HTMLElement, options: ReferenceWidgetRenderProperties) {\n\tconst { richObjectType, richObject, accessible, interactive } = options\n\tif (richObjectType === 'open-graph') {\n\t\treturn\n\t}\n\n\tif (!window._vue_richtext_widgets[richObjectType]) {\n\t\tlogger.error('Widget for rich object type ' + richObjectType + ' not registered')\n\t\treturn\n\t}\n\n\twindow._vue_richtext_widgets[richObjectType].callback(el, { richObjectType, richObject, accessible, interactive })\n}\n\n/**\n * Call the cleanup callback of the reference widget for given object type.\n *\n * @param richObjectType - The object type\n * @param el - The element\n */\nexport function destroyWidget(richObjectType: string, el: HTMLElement) {\n\tif (richObjectType === 'open-graph') {\n\t\treturn\n\t}\n\n\tif (!window._vue_richtext_widgets[richObjectType]) {\n\t\treturn\n\t}\n\n\twindow._vue_richtext_widgets[richObjectType].onDestroy(el)\n}\n\n/**\n * Check if a widget with the give id is registered.\n *\n * @param id - Id of the widget\n */\nexport function isWidgetRegistered(id: string): boolean {\n\treturn !!window._vue_richtext_widgets[id]\n}\n\n/**\n * Check if the given widget has an interactive view.\n *\n * @param id - Id of the widget\n */\nexport function hasInteractiveView(id: string): boolean {\n\treturn !!window._vue_richtext_widgets[id]?.hasInteractiveView\n}\n\n/**\n * Check if the widget supports full width.\n *\n * @param id - Id of the widget\n */\nexport function hasFullWidth(id: string): boolean {\n\treturn !!window._vue_richtext_widgets[id]?.fullWidth\n}\n","/**\n * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport { logger } from '../../utils/logger.ts'\n\nwindow._vue_richtext_custom_picker_elements ??= {}\nwindow._registerCustomPickerElement ??= registerCustomPickerElement\n\n/**\n * Representation of the render callback result\n * It contains a dom element and an object (Vue instance or other fancy things)\n */\nexport class NcCustomPickerRenderResult {\n\tpublic element: HTMLElement\n\tpublic object: object\n\n\t/**\n\t * @param element - The HTML element\n\t * @param object - The object\n\t */\n\tconstructor(element: HTMLElement, object: object) {\n\t\tthis.element = element\n\t\tthis.object = object\n\t}\n}\n\ninterface CustomPickerElementProps {\n\tproviderId: number\n\taccessible: boolean\n}\n\ntype CustomPickerElementRegistrationCallback = (el: HTMLElement, options: CustomPickerElementProps) => void\ntype CustomPickerElementDestroyCallback = (el: HTMLElement, result: NcCustomPickerRenderResult) => void\n\nexport interface CustomPickerElement {\n\tid: string\n\tsize: 'small' | 'normal' | 'large' | 'full'\n\n\tcallback: CustomPickerElementRegistrationCallback\n\tonDestroy: CustomPickerElementDestroyCallback\n}\n\n/**\n * @param id - Id of the element to check\n */\nexport function isCustomPickerElementRegistered(id: string): boolean {\n\treturn !!window._vue_richtext_custom_picker_elements[id]\n}\n\n/**\n * @param id - Id of the element to get\n */\nexport function getCustomPickerElementSize(id: string): 'small' | 'normal' | 'large' | 'full' | null {\n\tconst size = window._vue_richtext_custom_picker_elements[id]?.size\n\tif (['small', 'normal', 'large', 'full'].includes(size)) {\n\t\treturn size\n\t}\n\treturn null\n}\n\n/**\n * @param id - Id of element to register\n * @param callback - Render callback\n * @param onDestroy - Cleanup callback\n * @param size - Size of the element\n */\nexport function registerCustomPickerElement(\n\tid: string,\n\tcallback: CustomPickerElementRegistrationCallback,\n\tonDestroy: CustomPickerElementDestroyCallback = () => {},\n\tsize: CustomPickerElement['size'] = 'large',\n) {\n\tif (window._vue_richtext_custom_picker_elements[id]) {\n\t\tlogger.error(`Custom reference picker element for id ${id} already registered`)\n\t\treturn\n\t}\n\n\twindow._vue_richtext_custom_picker_elements[id] = {\n\t\tid,\n\t\tcallback,\n\t\tonDestroy,\n\t\tsize,\n\t}\n}\n\n/**\n * @param el - element to render to\n * @param options - Element options\n */\nexport function renderCustomPickerElement(el: HTMLElement, options: CustomPickerElementProps) {\n\tconst { providerId, accessible } = options\n\tif (!window._vue_richtext_custom_picker_elements[providerId]) {\n\t\tlogger.error(`Custom reference picker element for reference provider ID ${providerId} not registered`)\n\t\treturn\n\t}\n\n\treturn window._vue_richtext_custom_picker_elements[providerId]\n\t\t.callback(el, { providerId, accessible })\n}\n\n/**\n * @param providerId - Provider id of element to destroy\n * @param el - The element to destroy element from\n * @param renderResult - The render result\n */\nexport function destroyCustomPickerElement(providerId: string, el: HTMLElement, renderResult: NcCustomPickerRenderResult) {\n\tif (!window._vue_richtext_custom_picker_elements[providerId]) {\n\t\treturn\n\t}\n\n\twindow._vue_richtext_custom_picker_elements[providerId]\n\t\t.onDestroy(el, renderResult)\n}\n"],"names":["logger"],"mappings":";;AAyBA,OAAO,0BAA0B,CAAA;AACjC,OAAO,oBAAoB,CAAC,IAAY,UAAyC,WAA2C,UAAyC;AACpK,iBAAe,IAAI,UAAU,WAAW,KAAK;AAC9C;AAUO,SAAS,eACf,IACA,UACA,YAA4C,MAAM;AAAC,GACnD,OACC;AACD,QAAM,oBAAoB;AAAA,IACzB,oBAAoB;AAAA,IACpB,WAAW;AAAA,IACX,GAAG;AAAA,EAAA;AAGJ,MAAI,OAAO,sBAAsB,EAAE,GAAG;AACrCA,WAAAA,OAAO,MAAM,oCAAoC,EAAE,qBAAqB;AACxE;AAAA,EACD;AAEA,SAAO,sBAAsB,EAAE,IAAI;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,IACA,GAAG;AAAA,EAAA;AAEL;AAQO,SAAS,aAAa,IAAiB,SAA0C;AACvF,QAAM,EAAE,gBAAgB,YAAY,YAAY,gBAAgB;AAChE,MAAI,mBAAmB,cAAc;AACpC;AAAA,EACD;AAEA,MAAI,CAAC,OAAO,sBAAsB,cAAc,GAAG;AAClDA,WAAAA,OAAO,MAAM,iCAAiC,iBAAiB,iBAAiB;AAChF;AAAA,EACD;AAEA,SAAO,sBAAsB,cAAc,EAAE,SAAS,IAAI,EAAE,gBAAgB,YAAY,YAAY,aAAa;AAClH;AAQO,SAAS,cAAc,gBAAwB,IAAiB;AACtE,MAAI,mBAAmB,cAAc;AACpC;AAAA,EACD;AAEA,MAAI,CAAC,OAAO,sB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