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(task/compose): use proper line wrapped editor for title (#3995)

Seamus68d1b743fd04
diff --git a/js/app/packages/block-md/component/ComposeTask.tsx b/js/app/packages/block-md/component/ComposeTask.tsx
index 3fce66b6e5..71e04052db 100644
--- a/js/app/packages/block-md/component/ComposeTask.tsx
+++ b/js/app/packages/block-md/component/ComposeTask.tsx
@@ -2,14 +2,29 @@ import { useSplitLayout } from '@app/component/split-layout/layout';
import { useSplitPanelOrThrow } from '@app/component/split-layout/layoutUtils';
import { buildConfig } from '@core/component/LexicalMarkdown/builder/MarkdownConfigBuilder';
import { MarkdownShell } from '@core/component/LexicalMarkdown/builder/MarkdownShell';
+import { EmojiMenu } from '@core/component/LexicalMarkdown/component/menu/EmojiMenu';
+import { createLexicalWrapper } from '@core/component/LexicalMarkdown/context/LexicalWrapperContext';
+import {
+ autoRegister,
+ emojisPlugin,
+ singleLinePlugin,
+} from '@core/component/LexicalMarkdown/plugins';
import { addMediaFromFile } from '@core/component/LexicalMarkdown/plugins/media';
-import { initializeEditorEmpty } from '@core/component/LexicalMarkdown/utils';
+import { createMenuOperations } from '@core/component/LexicalMarkdown/shared/inlineMenu';
+import {
+ $getCaretRect,
+ forceSetTextContent,
+ initializeEditorEmpty,
+ isRectFlushWith,
+ trimWhitespace,
+} from '@core/component/LexicalMarkdown/utils';
import { toast } from '@core/component/Toast/Toast';
import { useUserId } from '@core/context/user';
import { registerHotkey, useHotkeyDOMScope } from '@core/hotkey/hotkeys';
import { createTask } from '@core/util/create';
import { filterMap } from '@core/util/list';
import { buildSimpleEntityUrl } from '@core/util/url';
+import { mergeRegister } from '@lexical/utils';
import ArrowSquareOutIcon from '@phosphor/arrow-square-out.svg';
import ArrowsOutIcon from '@phosphor/arrows-out.svg';
import PaperclipIcon from '@phosphor/paperclip.svg';
@@ -34,17 +49,32 @@ import { useUpsertToHistoryMutation } from '@queries/history/history';
import { refetchSoupEntity } from '@queries/soup/cache';
import { propertiesServiceClient } from '@service-properties/client';
import type { PropertyDefinition } from '@service-properties/generated/schemas/propertyDefinition';
+import { onElementConnect } from '@solid-primitives/lifecycle';
import { debounce } from '@solid-primitives/scheduled';
import { useQuery } from '@tanstack/solid-query';
import { Button, Hotkey, Scroll, ToggleSwitch } from '@ui';
-import type { LexicalEditor } from 'lexical';
import {
+ $getRoot,
+ $getSelection,
+ $isRangeSelection,
+ COMMAND_PRIORITY_NORMAL,
+ KEY_ARROW_DOWN_COMMAND,
+ KEY_ARROW_RIGHT_COMMAND,
+ KEY_ENTER_COMMAND,
+ KEY_ESCAPE_COMMAND,
+ type LexicalEditor,
+} from 'lexical';
+import {
+ type Accessor,
createEffect,
createSignal,
For,
+ on,
+ onCleanup,
onMount,
Show,
Suspense,
+ untrack,
} from 'solid-js';
import { createStore, reconcile, type Store, unwrap } from 'solid-js/store';
import { tabbable } from 'tabbable';
@@ -65,6 +95,177 @@ const COMPOSER_PROPERTIES = [
SYSTEM_PROPERTY_IDS.DUE_DATE,
];
+function composerTitleNavigationPlugin(
+ bodyEditor: Accessor<LexicalEditor | undefined>,
+ ignoreNavigation: Accessor<boolean>
+) {
+ const focusBodyStart = (event: KeyboardEvent | null) => {
+ if (ignoreNavigation()) return true;
+ const editor = bodyEditor();
+ if (!editor) return false;
+ event?.preventDefault();
+ event?.stopPropagation();
+ editor.update(() => {
+ const firstChild = $getRoot().getFirstChild();
+ firstChild?.selectStart();
+ });
+ editor.focus();
+ return true;
+ };
+
+ return (titleEditor: LexicalEditor) =>
+ mergeRegister(
+ titleEditor.registerCommand(
+ KEY_ENTER_COMMAND,
+ (event) => focusBodyStart(event),
+ COMMAND_PRIORITY_NORMAL
+ ),
+ titleEditor.registerCommand(
+ KEY_ARROW_DOWN_COMMAND,
+ (event) => {
+ if (ignoreNavigation()) return true;
+ const rect = titleEditor.getRootElement()?.getBoundingClientRect();
+ if (!rect) return false;
+ const caret = $getCaretRect() ?? rect;
+ if (!isRectFlushWith(caret, rect, 'bottom', 5)) return false;
+ return focusBodyStart(event);
+ },
+ COMMAND_PRIORITY_NORMAL
+ ),
+ titleEditor.registerCommand(
+ KEY_ARROW_RIGHT_COMMAND,
+ (event) => {
+ if (ignoreNavigation()) 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;
+ }
+ return focusBodyStart(event);
+ },
+ COMMAND_PRIORITY_NORMAL
+ )
+ );
+}
+
+function ComposeTaskTitleEditor(props: {
+ value: Accessor<string>;
+ onChange: (value: string) => void;
+ disabled: Accessor<boolean>;
+ bodyEditor: Accessor<LexicalEditor | undefined>;
+ containerRef: Accessor<HTMLDivElement | undefined>;
+ onUserInput: () => void;
+}) {
+ const [state, setState] = createSignal(props.value());
+ const [showPlaceholder, setShowPlaceholder] = createSignal(
+ props.value().trim() === ''
+ );
+ const [rootConnected, setRootConnected] = createSignal(false);
+
+ const { editor, plugins, cleanup } = createLexicalWrapper({
+ namespace: 'task-composer-title',
+ type: 'title',
+ isInteractable: () => !props.disabled(),
+ });
+
+ const emojiMenuOperations = createMenuOperations();
+
+ initializeEditorEmpty(editor);
+ forceSetTextContent(editor, props.value());
+
+ plugins
+ .plainText()
+ .history(400)
+ .use(singleLinePlugin())
+ .use(emojisPlugin({ menu: emojiMenuOperations }))
+ .use(
+ composerTitleNavigationPlugin(
+ props.bodyEditor,
+ emojiMenuOperations.isOpen
+ )
+ )
+ .state<string>(setState, 'plain');
+
+ plugins.onUpdate(({ editorState }) => {
+ if (!editorState) return;
+ setShowPlaceholder(
+ editorState.read(() => $getRoot().getTextContent().trim() === '')
+ );
+ });
+
+ createEffect(() => {
+ editor.setEditable(!props.disabled());
+ });
+
+ createEffect(
+ on(
+ state,
+ (next) => {
+ if (next === props.value()) return;
+ props.onUserInput();
+ props.onChange(next);
+ },
+ { defer: true }
+ )
+ );
+
+ createEffect(() => {
+ const next = props.value();
+ if (!untrack(rootConnected)) return;
+ if (next === untrack(state)) return;
+ forceSetTextContent(editor, next);
+ });
+
+ autoRegister(
+ editor.registerCommand(
+ KEY_ESCAPE_COMMAND,
+ (event) => {
+ props.containerRef()?.focus();
+ event?.preventDefault();
+ event?.stopPropagation();
+ return true;
+ },
+ COMMAND_PRIORITY_NORMAL
+ )
+ );
+
+ const onConnect = (el: HTMLDivElement) => {
+ editor.setRootElement(el);
+ setRootConnected(true);
+ onCleanup(() => {
+ trimWhitespace(editor, { trailing: true });
+ cleanup();
+ });
+ };
+
+ return (
+ <div class="relative w-full mb-4">
+ <div
+ contentEditable={!props.disabled()}
+ class="ph-no-capture w-full text-xl font-medium outline-none whitespace-pre-wrap wrap-break-words"
+ ref={(el) => {
+ onElementConnect(el, () => onConnect(el));
+ }}
+ />
+ <EmojiMenu
+ editor={editor}
+ menu={emojiMenuOperations}
+ useBlockBoundary={true}
+ />
+ <Show when={showPlaceholder()}>
+ <div class="pointer-events-none absolute top-1.5 text-xl font-medium text-ink-placeholder">
+ New task
+ </div>
+ </Show>
+ </div>
+ );
+}
+
/**
* Make a task and append props using the create_task endpoint.
* @param taskTitle Title string
@@ -706,37 +907,18 @@ export function ComposeTask(props: ComposeTaskProps) {
</Show>
</div>
<div class="flex-1 min-h-0 flex flex-col overflow-hidden">
- <div class="shrink-0 flex gap-2 items-center px-2">
- <input
- type="text"
- placeholder="New task"
- value={title()}
- onInput={(e) => {
- setTitle(e.currentTarget.value);
+ <div class="shrink-0 flex gap-2 items-start px-2">
+ <ComposeTaskTitleEditor
+ value={title}
+ onChange={setTitle}
+ disabled={isCreating}
+ bodyEditor={bodyEditor}
+ containerRef={containerRef}
+ onUserInput={() => {
if (errorMessage()) {
setErrorMessage('');
}
}}
- disabled={isCreating()}
- class="w-full py-2 text-xl font-medium placeholder-ink-placeholder disabled:opacity-50 truncate focus:overflow-visible"
- on:keydown={(e) => {
- if (e.key === 'Escape') {
- const container = containerRef();
- if (container) {
- container.focus();
- e.stopPropagation();
- e.preventDefault();
- }
- }
- if (e.key === 'Enter' || e.key === 'ArrowDown') {
- const editor = bodyEditor();
- if (editor) {
- e.stopPropagation();
- e.preventDefault();
- editor.focus(undefined, { defaultSelection: 'rootEnd' });
- }
- }
- }}
/>
</div>