Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import { activeElement } from '@app/signal/focus';
import { useSplitPanel } from '@components/app/split-layout/layoutUtils';
import { EmojiMenu } from '@core/component/LexicalMarkdown/component/menu/EmojiMenu';
import { TagsMenu } from '@core/component/LexicalMarkdown/component/menu/TagsMenu';
import { createLexicalWrapper } from '@core/component/LexicalMarkdown/context/LexicalWrapperContext';
import {
autoRegister,
emojisPlugin,
singleLinePlugin,
tagsPlugin,
} from '@core/component/LexicalMarkdown/plugins/';
import { $selectDocumentStart } from '@core/component/LexicalMarkdown/plugins/block-decorator-navigation';
import { createMenuOperations } from '@core/component/LexicalMarkdown/shared/inlineMenu';
import {
$getCaretRect,
forceSetTextContent,
initializeEditorEmpty,
isRectFlushWith,
trimWhitespace,
} from '@core/component/LexicalMarkdown/utils';
import { blockNameToDefaultFile } from '@core/constant/allBlocks';
import { fireAndForget } from '@core/util/fireAndForget';
import { onElementConnect } from '@core/util/onElementConnect';
import { createRenameDssEntityMutation } from '@entity';
import { mergeRegister } from '@lexical/utils';
import { useDocTags } from '@property/tags';
import { EntityType } from '@service-properties/generated/schemas/entityType';
import { debounce } from '@solid-primitives/scheduled';
import {
$createParagraphNode,
$createTextNode,
$getRoot,
$getSelection,
$isRangeSelection,
COMMAND_PRIORITY_NORMAL,
KEY_ARROW_DOWN_COMMAND,
KEY_ARROW_RIGHT_COMMAND,
KEY_ENTER_COMMAND,
KEY_ESCAPE_COMMAND,
KEY_TAB_COMMAND,
type LexicalEditor,
} from 'lexical';
import {
type Accessor,
createEffect,
createMemo,
createSignal,
on,
onCleanup,
Show,
untrack,
} from 'solid-js';
import { useMarkdownDocument } from '../context/markdown-document-context';
import { useMarkdownName } from './MarkdownNameProvider';
import { shouldAutofocusDocument } from './should-autofocus-document';
/**
* Use the plugin architecture to set up command handlers on both the
* title editor and the main editor. Pressing enter or the arrows can
* trigger navigation between the two editors if certain conditions are met.
* TODO (seamus): Consider making more robust if we get into a situation
* where there are three or more editors on one page.
*/
function titleNavigationPlugin(
documentEditor: LexicalEditor,
ignoreArrows: Accessor<boolean>
) {
return (titleEditor: LexicalEditor) =>
mergeRegister(
// Press enter in the title editor.
titleEditor.registerCommand(
KEY_ENTER_COMMAND,
(event) => {
if (!event) return false;
if (ignoreArrows()) return true;
event?.preventDefault();
// Prepend a new paragraph to the main editor.
documentEditor.update(() => {
const root = $getRoot();
const firstChild = root.getFirstChild();
const paragraph = $createParagraphNode();
paragraph.append($createTextNode(''));
firstChild
? firstChild.insertBefore(paragraph)
: root.append(paragraph);
paragraph.selectEnd();
});
return true;
},
COMMAND_PRIORITY_NORMAL
),
// Press arrow down in the title editor.
titleEditor.registerCommand(
KEY_ARROW_DOWN_COMMAND,
(event: KeyboardEvent) => {
if (ignoreArrows()) return true;
const rect = titleEditor.getRootElement()?.getBoundingClientRect();
if (!rect) return false;
const caret = $getCaretRect() ?? rect;
if (!isRectFlushWith(caret, rect, 'bottom', 5)) return false;
event?.preventDefault();
// A node selection places no DOM caret, so focus the body first.
documentEditor.getRootElement()?.focus({ preventScroll: true });
documentEditor.update($selectDocumentStart);
return true;
},
COMMAND_PRIORITY_NORMAL
),
// Press right from the end of the title.
titleEditor.registerCommand(
KEY_ARROW_RIGHT_COMMAND,
(event: KeyboardEvent) => {
if (ignoreArrows()) return true;
const selection = $getSelection();
if (!$isRangeSelection(selection) || !selection.isCollapsed())
return false;
const anchorNode = selection.anchor.getNode();
const len = anchorNode.getTextContent().length;
if (selection.anchor.offset !== len) return false;
if (anchorNode.getParent()?.getLastChild() !== anchorNode)
return false;
event?.preventDefault();
// A node selection places no DOM caret, so focus the body first.
documentEditor.getRootElement()?.focus({ preventScroll: true });
documentEditor.update($selectDocumentStart);
return true;
},
COMMAND_PRIORITY_NORMAL
),
// Press tab in the title. Without this the browser moves focus to the
// tag controls below the title instead of the body.
titleEditor.registerCommand(
KEY_TAB_COMMAND,
(event: KeyboardEvent) => {
if (ignoreArrows()) return true;
if (event.shiftKey) return false;
event.preventDefault();
documentEditor.getRootElement()?.focus({ preventScroll: true });
documentEditor.update($selectDocumentStart);
return true;
},
COMMAND_PRIORITY_NORMAL
)
);
}
export function TitleEditor(props: { autoFocusOnMount?: boolean } = {}) {
const {
documentId,
element: blockElement,
kind,
documentSource,
permissions,
state: documentState,
} = useMarkdownDocument();
const panel = useSplitPanel();
const canEdit = permissions.canEdit;
const { md: mdData, setMd: setMdData } = documentState.editor;
const renameDocumentMutation = createRenameDssEntityMutation();
const {
persistedName: persistedDocumentName,
editorName: mdDocumentName,
setOptimisticName,
} = useMarkdownName();
const [showFallback, setShowFallback] = createSignal(true);
const [titlePlaceholder] = createSignal<string>();
const [titleFocused, setTitleFocused] = createSignal(false);
const blockId = documentId();
const documentKind = kind();
const entityBlockName = documentKind === 'document' ? 'md' : documentKind;
const documentTags = useDocTags(
blockId,
documentKind === 'task' ? EntityType.TASK : EntityType.DOCUMENT
);
const titlePlaceholderFallback = blockNameToDefaultFile(entityBlockName);
let pendingRename:
| {
newName: string;
oldName: string;
}
| undefined;
const flushPendingRename = () => {
const next = pendingRename;
pendingRename = undefined;
if (!next || !canEdit()) return;
renameDocumentMutation.mutate({
entity: {
type: 'document',
id: blockId,
name: next.oldName,
},
newName: next.newName,
});
};
const scheduleRename = (newName: string, oldName: string) => {
if (newName === oldName) {
pendingRename = undefined;
debouncedFlushRename.clear();
return;
}
pendingRename = { newName, oldName };
debouncedFlushRename();
};
const debouncedFlushRename = debounce(flushPendingRename, 400);
// Unmount cancels the debounce timer, so save a pending rename now.
onCleanup(flushPendingRename);
const [state, setState] = createSignal('');
const [initialized, setInitialized] = createSignal(false);
const { editor, plugins, cleanup } = createLexicalWrapper({
namespace: 'block-md-title',
type: 'title',
isInteractable: createMemo(() => {
return canEdit() ?? false;
}),
});
onCleanup(cleanup);
initializeEditorEmpty(editor);
setMdData('titleEditor', editor);
const emojiMenuOperations = createMenuOperations();
const tagMenuOperations = createMenuOperations();
const inlineMenuOpen = () =>
emojiMenuOperations.isOpen() || tagMenuOperations.isOpen();
plugins
.plainText()
.history(400)
.use(singleLinePlugin())
.use(
emojisPlugin({
menu: emojiMenuOperations,
})
)
.use(
tagsPlugin({
menu: tagMenuOperations,
insertTags: false,
onCreateTag: (tag) => {
if (!canEdit()) return;
fireAndForget(
documentTags.applyTag(tag.scope, tag.optionId),
'document-tags'
);
},
})
)
.state<string>(setState, 'plain');
plugins.onUpdate(({ editorState }) => {
if (!editorState) return;
const isEmpty = editorState.read(() => {
return $getRoot().getTextContent() === '';
});
setShowFallback(isEmpty);
});
let selfChangedTitle = false;
// Wait for the main editor to be mounted, then register the navigate plugin.
createEffect(() => {
const mainDocumentEditor = mdData.editor;
if (!mainDocumentEditor) return;
plugins.use(titleNavigationPlugin(mainDocumentEditor, inlineMenuOpen));
});
function onBlur() {
setTitleFocused(false);
trimWhitespace(editor, { trailing: true });
}
const [rootConnected, setRootConnected] = createSignal(false);
const onConnect = (el: HTMLDivElement) => {
editor.setRootElement(el);
const onFocus = () => setTitleFocused(true);
el.addEventListener('focus', onFocus);
el.addEventListener('blur', onBlur);
setRootConnected(true);
onCleanup(() => {
el.removeEventListener('focus', onFocus);
el.removeEventListener('blur', onBlur);
});
};
createEffect(() => {
editor.setEditable(canEdit() ?? false);
});
const dataReady = () => documentSource().type !== 'loading';
const hasLocalTitleEdit = createMemo(() => {
if (!titleFocused()) return false;
return state().trim() !== (mdDocumentName() ?? '');
});
createEffect(
on(
() => ({ docName: persistedDocumentName(), ready: dataReady() }),
({ docName, ready }) => {
const currentState = untrack(state);
if (
ready &&
docName !== undefined &&
docName !== currentState.trim() &&
!selfChangedTitle &&
!untrack(hasLocalTitleEdit)
) {
forceSetTextContent(editor, docName);
}
selfChangedTitle = false;
}
)
);
createEffect(() => {
const currentState = state();
if (untrack(inlineMenuOpen)) return;
if (!untrack(initialized)) {
setInitialized(true);
return;
}
const nextName = currentState.trim();
if (nextName !== untrack(mdDocumentName)) {
setOptimisticName(nextName);
}
const persistedName = untrack(persistedDocumentName) ?? '';
if (nextName !== persistedName) {
selfChangedTitle = true;
if (canEdit()) {
scheduleRename(nextName, persistedName);
}
} else {
scheduleRename(nextName, persistedName);
}
});
createEffect(() => {
if (mdDocumentName() === persistedDocumentName()) {
setOptimisticName(undefined);
}
});
autoRegister(
editor.registerCommand(
KEY_ESCAPE_COMMAND,
() => {
editor.blur();
return true;
},
COMMAND_PRIORITY_NORMAL
)
);
// Autofocus a new document's empty title once its metadata has loaded.
let didAutoFocus = false;
createEffect(() => {
activeElement();
const name = mdDocumentName();
if (
!didAutoFocus &&
props.autoFocusOnMount &&
rootConnected() &&
dataReady() &&
name !== undefined
) {
if (name === '') {
if (
shouldAutofocusDocument(
blockElement() ?? null,
panel?.panelRef() ?? null
)
) {
didAutoFocus = true;
editor.focus();
}
} else {
didAutoFocus = true;
}
}
});
return (
<div class="relative">
<div
contentEditable={canEdit() ?? false}
class="ph-no-capture text-2xl font-semibold"
classList={{
'select-auto': !canEdit(),
}}
ref={(el) => {
onElementConnect(el, () => onConnect(el));
}}
/>
<EmojiMenu
editor={editor}
menu={emojiMenuOperations}
useBlockBoundary={true}
/>
<TagsMenu
editor={editor}
menu={tagMenuOperations}
useBlockBoundary={true}
/>
<Show when={showFallback()}>
<div class="text-2xl font-semibold text-ink-placeholder absolute top-0 pointer-events-none">
{titlePlaceholder() ?? titlePlaceholderFallback}
</div>
</Show>
</div>
);
}