Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(md): improve accessory glue to responsiveness (#4154)

Seamus42956f5e5938
diff --git a/js/app/packages/channel/Message/ChannelMessage.tsx b/js/app/packages/channel/Message/ChannelMessage.tsx
index 43edf66478..6f7a5cd5b3 100644
--- a/js/app/packages/channel/Message/ChannelMessage.tsx
+++ b/js/app/packages/channel/Message/ChannelMessage.tsx
@@ -161,6 +161,7 @@ function GroupedMessageLayout(props: {
<MessageContentSlot
channelId={props.channelId}
messageEditor={props.messageEditor}
+ class="min-w-0 flex-1"
/>
{/* TODO (seamus): hiding the grouped meta for now */}
{/*<GroupedMeta messageEditor={props.messageEditor} />*/}
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/accessory/CodeBoxAccessory.tsx b/js/app/packages/core/component/LexicalMarkdown/component/accessory/CodeBoxAccessory.tsx
index fba72e89b6..5939b41f6f 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/accessory/CodeBoxAccessory.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/accessory/CodeBoxAccessory.tsx
@@ -46,8 +46,6 @@ import { Dynamic } from 'solid-js/web';
import { glueToElement } from '../../directive/glueToElement';
import { autoRegister } from '../../plugins/shared/utils';
-false && glueToElement;
-
const LanguageIcons: Record<
SupportedLanguage,
Component<{ class?: string }>
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx b/js/app/packages/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
index 33925c8414..64e4aff7e4 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
@@ -107,6 +107,14 @@ type Token = {
};
const CodeHighlightShim = {
+ createEmptyLinePlaceholder: (): Node => {
+ const spanNode = document.createElement('span');
+ spanNode.classList.add('md-code-empty-line');
+ spanNode.setAttribute('aria-hidden', 'true');
+ spanNode.innerText = '\u200B';
+ return spanNode;
+ },
+
/**
* Get highlight spans from the Prism tokens.
*/
@@ -116,6 +124,7 @@ const CodeHighlightShim = {
theme: EditorThemeClasses
): Node[] => {
const nodes: Node[] = [];
+ let atLineStart = true;
for (const token of tokens) {
if (typeof token === 'string') {
const partials = token.split(/(\n|\t)/);
@@ -123,12 +132,17 @@ const CodeHighlightShim = {
for (let i = 0; i < partialsLength; i++) {
const part = partials[i];
if (part === '\n' || part === '\r\n') {
+ if (atLineStart) {
+ nodes.push(CodeHighlightShim.createEmptyLinePlaceholder());
+ }
nodes.push(document.createElement('br'));
+ atLineStart = true;
} else if (part === '\t') {
const tabNode = document.createElement('span');
const className = theme.tab;
if (className) tabNode.classList.add(className);
nodes.push(tabNode);
+ atLineStart = false;
} else if (part.length > 0) {
const spanNode = document.createElement('span');
const className = type
@@ -137,6 +151,7 @@ const CodeHighlightShim = {
if (className) spanNode.classList.add(className);
spanNode.innerText = part;
nodes.push(spanNode);
+ atLineStart = false;
}
}
} else {
@@ -152,6 +167,9 @@ const CodeHighlightShim = {
}
}
}
+ if (nodes.length === 0) {
+ nodes.push(CodeHighlightShim.createEmptyLinePlaceholder());
+ }
return nodes;
},
diff --git a/js/app/packages/core/component/LexicalMarkdown/directive/glueToElement.ts b/js/app/packages/core/component/LexicalMarkdown/directive/glueToElement.ts
index a9b9969651..7ceaaf89c7 100644
--- a/js/app/packages/core/component/LexicalMarkdown/directive/glueToElement.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/directive/glueToElement.ts
@@ -2,6 +2,7 @@ import { autoUpdate, computePosition, hide } from '@floating-ui/dom';
import type { LexicalEditor } from 'lexical';
import type { Accessor, JSX } from 'solid-js';
import { createEffect, onCleanup } from 'solid-js';
+import { registerEditorMutationObserver } from '../plugins/shared/utils';
type GlueToElementProps = {
editor: LexicalEditor;
@@ -33,12 +34,23 @@ export function glueToElement(
style(floatingElement, { position: 'absolute' });
let cleanupAutoUpdate: () => void = () => {};
+ let cleanupEditorMutationObserver: () => void = () => {};
+ let animationFrame: number | undefined;
+
+ const scheduleUpdatePosition = () => {
+ if (animationFrame !== undefined) return;
+ animationFrame = requestAnimationFrame(() => {
+ animationFrame = undefined;
+ void updatePosition();
+ });
+ };
async function updatePosition() {
const el = propAccessor().element();
const root = propAccessor().editor.getRootElement();
+ const mount = floatingElement.offsetParent as HTMLElement | null;
- if (!el || !root) {
+ if (!el || !root || !mount) {
style(floatingElement, { display: 'none' });
return;
}
@@ -48,11 +60,12 @@ export function glueToElement(
});
const rect = el.getBoundingClientRect();
- const rootRect = root.getBoundingClientRect();
- const offsetLeft = rect.left - rootRect.left;
- const offsetTop = rect.top - rootRect.top;
+ const mountRect = mount.getBoundingClientRect();
+ const offsetLeft = rect.left - mountRect.left + mount.scrollLeft;
+ const offsetTop = rect.top - mountRect.top + mount.scrollTop;
style(floatingElement, {
+ display: '',
left: `${offsetLeft}px`,
top: `${offsetTop}px`,
width: `${rect.width}px`,
@@ -63,6 +76,7 @@ export function glueToElement(
createEffect(() => {
cleanupAutoUpdate();
+ cleanupEditorMutationObserver();
const referenceEl = propAccessor().element() ?? null;
if (!referenceEl) return;
cleanupAutoUpdate = autoUpdate(
@@ -70,9 +84,17 @@ export function glueToElement(
floatingElement,
updatePosition
);
+ cleanupEditorMutationObserver = registerEditorMutationObserver(
+ propAccessor().editor,
+ scheduleUpdatePosition
+ );
});
onCleanup(() => {
cleanupAutoUpdate();
+ cleanupEditorMutationObserver();
+ if (animationFrame !== undefined) {
+ cancelAnimationFrame(animationFrame);
+ }
});
}
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/node-accessory/nodeAccessoryPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/node-accessory/nodeAccessoryPlugin.ts
index 8793e01ece..b24bb12684 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/node-accessory/nodeAccessoryPlugin.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/node-accessory/nodeAccessoryPlugin.ts
@@ -43,7 +43,7 @@ type NodeAccessoryPluginProps<T extends LexicalNode> = {
setStore: SetStoreFunction<AccessoryStore>;
};
-function registerNodeAcessoryPlugin<T extends LexicalNode>(
+function registerNodeAccessoryPlugin<T extends LexicalNode>(
editor: LexicalEditor,
props: NodeAccessoryPluginProps<T>
) {
@@ -68,6 +68,7 @@ function registerNodeAcessoryPlugin<T extends LexicalNode>(
editor.registerCommand(
FORCE_REFRESH_ACCESSORIES_COMMAND,
(payload) => {
+ console.log('FORCE_REFRESH_ACCESSORIES_COMMAND', payload);
for (const key of payload) {
if (key in props.store) {
const temp = { ...unwrap(props.store[key]) };
@@ -89,7 +90,7 @@ function registerNodeAcessoryPlugin<T extends LexicalNode>(
* in the editor you would have to define a custom paragraph class that manages rendering
* itself and a decorator and then override the default paragraph node. With this approach
* you can attach the menu to the existing paragraph node. In order to use it the containing
- * editor will also need have a <NodeAcessoryRenderer /> as a child.
+ * editor will also need have a <NodeAccessoryRenderer /> as a child.
* @param props.klass The Lexical node class to bind to.
* @param props.component The component to render for the node. This is a simple component
* that will only receive a ref to the editor and the node key of its attached node.
@@ -97,5 +98,5 @@ function registerNodeAcessoryPlugin<T extends LexicalNode>(
export function nodeAccessoryPlugin<T extends LexicalNode>(
props: NodeAccessoryPluginProps<T>
) {
- return (editor: LexicalEditor) => registerNodeAcessoryPlugin(editor, props);
+ return (editor: LexicalEditor) => registerNodeAccessoryPlugin(editor, props);
}
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/shared/utils.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/shared/utils.ts
index eb2d7500ec..9161c8c6ef 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/shared/utils.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/shared/utils.ts
@@ -31,6 +31,7 @@ export function mapRegisterDelete(
}
type WidthChangeCallback = (width: number) => void;
+type EditorMutationCallback = () => void;
const observersByEditor = new WeakMap<
LexicalEditor,
@@ -40,6 +41,15 @@ const observersByEditor = new WeakMap<
}
>();
+const mutationObserversByEditor = new WeakMap<
+ LexicalEditor,
+ {
+ observer: MutationObserver;
+ callbacks: Set<EditorMutationCallback>;
+ cleanupRootListener: () => void;
+ }
+>();
+
/**
* Register a callback that runs when the width of the editor root changes. If a
* a selector is provided, then the closest matching parent will be observed
@@ -100,6 +110,59 @@ export function registerEditorWidthObserver(
);
}
+/**
+ * Register a callback that runs when the editor root mutates. Observing is
+ * shared per editor so multiple floating controls do not each attach their own
+ * MutationObserver to the same root.
+ */
+export function registerEditorMutationObserver(
+ editor: LexicalEditor,
+ onMutation: EditorMutationCallback
+) {
+ let editorObserver = mutationObserversByEditor.get(editor);
+
+ if (!editorObserver) {
+ const callbacks = new Set<EditorMutationCallback>();
+ const observer = new MutationObserver(() => {
+ callbacks.forEach((callback) => callback());
+ });
+
+ const cleanupRootListener = editor.registerRootListener(
+ (root, prevRoot) => {
+ if (prevRoot) {
+ observer.disconnect();
+ }
+ if (root) {
+ observer.observe(root, {
+ attributes: true,
+ childList: true,
+ characterData: true,
+ subtree: true,
+ });
+ }
+ }
+ );
+
+ editorObserver = { observer, callbacks, cleanupRootListener };
+ mutationObserversByEditor.set(editor, editorObserver);
+ }
+
+ editorObserver.callbacks.add(onMutation);
+
+ return () => {
+ const editorObserver = mutationObserversByEditor.get(editor);
+ if (!editorObserver) return;
+
+ editorObserver.callbacks.delete(onMutation);
+
+ if (editorObserver.callbacks.size === 0) {
+ editorObserver.observer.disconnect();
+ editorObserver.cleanupRootListener();
+ mutationObserversByEditor.delete(editor);
+ }
+ };
+}
+
/**
* Take a signal that might be undefined or a lexical editor and register some
* plugin-style cleanup closure on it. As long as this is called in the
diff --git a/js/app/packages/core/component/LexicalMarkdown/styles.css b/js/app/packages/core/component/LexicalMarkdown/styles.css
index 8a85a3c87e..64bed65267 100644
--- a/js/app/packages/core/component/LexicalMarkdown/styles.css
+++ b/js/app/packages/core/component/LexicalMarkdown/styles.css
@@ -206,10 +206,18 @@ ol.static-md.depth-6 > li:not(.list-none)::before {
.md-static-code-container:not(.no-accessory) {
padding-top: 3.5rem;
+ align-self: stretch;
+ min-width: 0;
}
.md-static-code-container .md-code-box:not(.no-accessory) {
margin-top: -1.5rem;
+ min-height: 2.75rem;
+}
+
+.md-code-empty-line {
+ display: inline-block;
+ min-height: 1lh;
}
.md-code-box.no-accessory .md-code-box-header {