@/utils#getEditorRoot TypeScript Examples
The following examples show how to use
@/utils#getEditorRoot.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useHotKeys.ts From easy-email with MIT License | 6 votes |
function isContentEditFocus() {
const isShadowRootFocus = document.activeElement === getEditorRoot();
if (isShadowRootFocus) {
if (
getEditorRoot()?.shadowRoot?.activeElement?.getAttribute(
'contenteditable'
) === 'true'
) {
return true;
}
} else {
if (
['input', 'textarea'].includes(
document.activeElement?.tagName.toLocaleLowerCase() || ''
) ||
document.activeElement?.getAttribute('contenteditable') === 'true'
) {
return true;
}
}
return false;
}
Example #2
Source File: useHotKeys.ts From easy-email with MIT License | 5 votes |
export function useHotKeys() {
const { redo, undo, removeBlock } = useBlock();
const { focusIdx, setFocusIdx } = useFocusIdx();
const {
formState: { values },
} = useEditorContext();
const root = getShadowRoot();
// redo/undo
useEffect(() => {
const onKeyDown = (ev: KeyboardEvent) => {
if (isContentEditFocus()) return;
if (isHotkey('mod+z', ev)) {
ev.preventDefault();
undo();
}
if (isHotkey('mod+y', ev) || isHotkey('mod+shift+z', ev)) {
ev.preventDefault();
redo();
}
};
window.addEventListener('keydown', onKeyDown);
return () => {
window.removeEventListener('keydown', onKeyDown);
};
}, [redo, undo]);
// delete
useEffect(() => {
const onKeyDown = (ev: KeyboardEvent) => {
const isShadowRootFocus = document.activeElement === getEditorRoot();
if (!isShadowRootFocus) return;
if (isContentEditFocus()) return;
// if (isHotkey('delete', ev) || isHotkey('backspace', ev)) {
// removeBlock(focusIdx);
// }
};
window.addEventListener('keydown', onKeyDown);
return () => {
window.removeEventListener('keydown', onKeyDown);
};
}, [focusIdx, removeBlock]);
// focus
useEffect(() => {
const onKeyDown = (ev: KeyboardEvent) => {
const isShadowRootFocus = document.activeElement === getEditorRoot();
if (!isShadowRootFocus) return;
if (isHotkey('tab', ev) || isHotkey('shift+tab', ev)) {
setTimeout(() => {
const activeElement = getShadowRoot().activeElement;
if (activeElement instanceof HTMLElement) {
const blockNode = getBlockNodeByChildEle(activeElement);
if (blockNode) {
const idx = getNodeIdxFromClassName(blockNode.classList)!;
setFocusIdx(idx);
}
}
}, 0);
}
};
window.addEventListener('keydown', onKeyDown);
return () => {
window.removeEventListener('keydown', onKeyDown);
};
}, [focusIdx, removeBlock, setFocusIdx, values]);
}
Example #3
Source File: MjmlDomRender.tsx From easy-email with MIT License | 4 votes |
export function MjmlDomRender() {
const { pageData: content } = useEditorContext();
const [pageData, setPageData] = useState<IPage | null>(null);
const [ref, setRef] = useState<HTMLDivElement | null>(null);
const { dashed, mergeTags, enabledMergeTagsBadge } = useEditorProps();
const [isTextFocus, setIsTextFocus] = useState(false);
const isTextFocusing =
document.activeElement === getEditorRoot() &&
getShadowRoot().activeElement?.getAttribute('contenteditable') === 'true';
useEffect(() => {
if (!isTextFocus && !isEqual(content, pageData)) {
setPageData(cloneDeep(content));
}
}, [content, pageData, setPageData, isTextFocus]);
useEffect(() => {
setIsTextFocus(isTextFocusing);
}, [isTextFocusing]);
useEffect(() => {
const onClick = (e: MouseEvent) => {
if (getEditorRoot()?.contains(e.target as Node)) {
return;
}
const fixedContainer = document.getElementById(FIXED_CONTAINER_ID);
if (fixedContainer?.contains(e.target as Node)) {
return;
}
setIsTextFocus(false);
};
window.addEventListener('click', onClick);
return () => {
window.removeEventListener('click', onClick);
};
}, []);
useEffect(() => {
const root = getShadowRoot();
if (!root) return;
const onClick = (e: Event) => {
const isFocusing =
getShadowRoot().activeElement?.getAttribute('contenteditable') ===
'true';
if (isFocusing) {
setIsTextFocus(true);
}
};
root.addEventListener('click', onClick);
return () => {
root.removeEventListener('click', onClick);
};
}, []);
const html = useMemo(() => {
if (!pageData) return '';
const renderHtml = mjml(
JsonToMjml({
data: pageData,
idx: getPageIdx(),
context: pageData,
mode: 'testing',
dataSource: cloneDeep(mergeTags),
})
).html;
return renderHtml;
}, [mergeTags, pageData]);
return useMemo(() => {
return (
<div
{
...{
[DATA_RENDER_COUNT]: count++
}
}
data-dashed={dashed}
ref={setRef}
style={{
outline: 'none',
position: 'relative',
}}
role='tabpanel'
tabIndex={0}
>
{ref &&
createPortal(
HtmlStringToReactNodes(html, {
enabledMergeTagsBadge: Boolean(enabledMergeTagsBadge),
}),
ref
)}
</div>
);
}, [dashed, ref, html, enabledMergeTagsBadge]);
}