fix(Docs): Don't inject toolbar or component scripts multiple times
@@ -79,6 +79,14 @@ const injectExecutableToolbar = () => {
const doc = getPreviewDoc()
const article = doc?.querySelector('[data-itemscope="root"]')
+ const toolbarEl =
+ article?.querySelectorAll('stencila-executable-document-toolbar') ?? []
+
+ // Toolbar has already been injected, so terminate function early
+ if (toolbarEl.length > 0) {
+ return
+ }
+
if (doc && article) {
const toolbar = doc.createElement('stencila-executable-document-toolbar')
toolbar.setAttribute('source-url', '#')
@@ -87,6 +95,16 @@ const injectExecutableToolbar = () => {
}
}
+const previewAssestsPresent = (): boolean => {
+ const doc = getPreviewDoc()
+ const scripts =
+ doc?.querySelectorAll(
+ 'script[src="https://unpkg.com/@stencila/components@latest/dist/stencila-components/stencila-components.js"]'
+ ) ?? []
+
+ return scripts.length > 0
+}
+
/**
* Inject necessary stylesheets and scripts to fully render a document
* Currently this function inject the scripts for Stencila Components. Note that you will need to trigger the
@@ -97,7 +115,7 @@ const injectExecutableToolbar = () => {
export const injectPreviewAssets = (): void => {
const previewHead = getPreviewHead()
- if (previewHead != null) {
+ if (previewHead != null && !previewAssestsPresent()) {
const stencilaComponentsES6 = create('script')
stencilaComponentsES6.setAttribute('type', 'module')
stencilaComponentsES6.setAttribute(
