From 0ffdc5f6a5f74b11ee1d9fd2b65595bf5dea9e22 Mon Sep 17 00:00:00 2001 From: Martynas Bagdonas Date: Wed, 13 Sep 2023 13:47:25 +0200 Subject: [PATCH] Enable injecting custom content into various reader UI parts --- .../components/common/custom-sections.js | 26 +++++++++++++++++ src/common/components/common/preview.js | 2 ++ src/common/components/toolbar.js | 2 ++ src/common/components/view/selection-popup.js | 2 ++ src/common/context-menu.js | 28 ++++++++++++++++--- src/common/stylesheets/base/_base.scss | 4 +++ .../stylesheets/components/_page-popup.scss | 11 ++++++++ .../stylesheets/components/_preview.scss | 6 ++++ .../stylesheets/components/_toolbar.scss | 6 ++++ 9 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/common/components/common/custom-sections.js diff --git a/src/common/components/common/custom-sections.js b/src/common/components/common/custom-sections.js new file mode 100644 index 00000000..27e1f884 --- /dev/null +++ b/src/common/components/common/custom-sections.js @@ -0,0 +1,26 @@ +import React, { memo, useEffect, useRef } from 'react'; + +let CustomSections = memo(({ type, ...props }) => { + let sectionRef = useRef(); + useEffect(() => { + sectionRef.current.replaceChildren(); + let finished = false; + let append = (...args) => { + if (finished) { + throw new Error('Append must be called directly and synchronously in the event'); + } + let section = document.createElement('div'); + section.className = 'section'; + section.append(...args); + sectionRef.current.append(section); + }; + let event = new CustomEvent(`customEvent`, { detail: { type: `render${type}`, doc: document, append, params: props } }); + window.dispatchEvent(event); + finished = true; + }); + return ( +
+ ); +}); + +export default CustomSections; diff --git a/src/common/components/common/preview.js b/src/common/components/common/preview.js index e3d7e374..b23624dd 100644 --- a/src/common/components/common/preview.js +++ b/src/common/components/common/preview.js @@ -6,6 +6,7 @@ import ExpandableEditor from './expandable-editor'; import { IconHighlight, IconUnderline, IconNote, IconArea, IconInk, IconText } from './icons'; import { getPopupCoordinatesFromClickEvent } from '../../lib/utilities'; import { ReaderContext } from '../../reader'; +import CustomSections from './custom-sections'; // TODO: Don't allow to select UI text in popup header and footer @@ -289,6 +290,7 @@ export function SidebarPreview(props) { {annotation.authorName}
)} +