-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Julius Härtl <[email protected]>
- Loading branch information
1 parent
4d8d4e7
commit 78fb885
Showing
8 changed files
with
129 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
interface WidgetRenderProperties { | ||
richObjectType: string; | ||
richObject: object; | ||
accessible: boolean; | ||
interactive: boolean; | ||
} | ||
|
||
type widgetRenderCallback = (el: HTMLElement, properties: WidgetRenderProperties) => void; | ||
type widgetDestroyCallback = (el: HTMLElement) => void; | ||
|
||
interface WidgetProps { | ||
id: string; | ||
hasInteractiveView: boolean; | ||
callback: widgetRenderCallback; | ||
onDestroy: widgetDestroyCallback; | ||
} | ||
|
||
interface WidgetPropsOptional { | ||
hasInteractiveView?: boolean; | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
_vue_richtext_widgets: Record<string, WidgetProps>; | ||
_registerWidget: (id: string, callback: widgetRenderCallback, onDestroy: widgetDestroyCallback, props: WidgetPropsOptional) => void; | ||
} | ||
} | ||
|
||
if (!window._vue_richtext_widgets) { | ||
window._vue_richtext_widgets = {} | ||
} | ||
|
||
const isWidgetRegistered = (id: string) => { | ||
return !!window._vue_richtext_widgets[id] | ||
} | ||
|
||
const hasInteractiveView = (id: string) => { | ||
return !!window._vue_richtext_widgets[id]?.hasInteractiveView | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const registerWidget = (id: string, callback: widgetRenderCallback, onDestroy = (el: HTMLElement) => {}, props: WidgetPropsOptional) => { | ||
const propsWithDefaults = { | ||
hasInteractiveView: true, | ||
...props, | ||
} | ||
|
||
if (window._vue_richtext_widgets[id]) { | ||
console.error('Widget for id ' + id + ' already registered') | ||
return | ||
} | ||
|
||
window._vue_richtext_widgets[id] = { | ||
id, | ||
callback, | ||
onDestroy, | ||
...propsWithDefaults, | ||
} | ||
} | ||
|
||
const renderWidget = (el: HTMLElement, { richObjectType, richObject, accessible, interactive }) => { | ||
if (richObjectType === 'open-graph') { | ||
return | ||
} | ||
|
||
if (!window._vue_richtext_widgets[richObjectType]) { | ||
console.error('Widget for rich object type ' + richObjectType + ' not registered') | ||
return | ||
} | ||
|
||
window._vue_richtext_widgets[richObjectType].callback(el, { richObjectType, richObject, accessible, interactive }) | ||
} | ||
|
||
const destroyWidget = (richObjectType: string, el: HTMLElement) => { | ||
if (richObjectType === 'open-graph') { | ||
return | ||
} | ||
|
||
if (!window._vue_richtext_widgets[richObjectType]) { | ||
return | ||
} | ||
|
||
window._vue_richtext_widgets[richObjectType].onDestroy(el) | ||
} | ||
|
||
window._registerWidget = (id: string, callback: widgetRenderCallback, onDestroy: widgetDestroyCallback, props: WidgetPropsOptional) => { | ||
registerWidget(id, callback, onDestroy, props) | ||
} | ||
|
||
export { | ||
registerWidget, | ||
renderWidget, | ||
destroyWidget, | ||
isWidgetRegistered, | ||
hasInteractiveView, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters