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[mobile]: mobile typing issues in the Markdown editor (#1370)

Peter Chinmanb342f9e31070
diff --git a/js/app/packages/block-md/component/FrontMatterProperties.tsx b/js/app/packages/block-md/component/FrontMatterProperties.tsx
index 4f64771f28..1218e79123 100644
--- a/js/app/packages/block-md/component/FrontMatterProperties.tsx
+++ b/js/app/packages/block-md/component/FrontMatterProperties.tsx
@@ -1,4 +1,5 @@
import { useBlockAliasedName, useBlockId } from '@core/block';
+import { isMobile } from '@core/mobile/isMobile';
import {
$getPinnedProperties,
ADD_PINNED_PROPERTY_COMMAND,
@@ -66,9 +67,10 @@ export function FrontMatterProperties(props: FrontMatterPropertiesProps) {
);
// Track expanded/collapsed state from persisted preference
+ // Default to collapsed on mobile, expanded on desktop
const isExpanded = createMemo(() => {
const preference = frontMatterPreference[blockId];
- return preference === undefined ? true : preference;
+ return preference === undefined ? !isMobile() : preference;
});
const toggleExpanded = () => {
diff --git a/js/app/packages/block-md/component/MarkdownEditor.tsx b/js/app/packages/block-md/component/MarkdownEditor.tsx
index f5c7926c39..2d7e5c745d 100644
--- a/js/app/packages/block-md/component/MarkdownEditor.tsx
+++ b/js/app/packages/block-md/component/MarkdownEditor.tsx
@@ -108,6 +108,7 @@ import {
createFilesReadyHandler,
getDragDropPosition,
} from '@core/component/LexicalMarkdown/utils/fileUploadUtils';
+import { iosCursorScrollPlugin } from '@core/component/LexicalMarkdown/plugins/ios-cursor-scroll';
import { ScopedPortal } from '@core/component/ScopedPortal';
import { toast } from '@core/component/Toast/Toast';
import { fileTypeToBlockName } from '@core/constant/allBlocks';
@@ -176,6 +177,9 @@ import type { MarkdownRewriteOutput } from '../signal/rewriteSignal';
import { useBlockSave, useSaveMarkdownDocument } from '../signal/save';
import { MarkdownCollabProvider } from './MarkdownCollabProvider';
import { MarkdownPopup } from './MarkdownPopup';
+import { isMobile } from '@core/mobile/isMobile';
+import { isIOS } from '@solid-primitives/platform';
+import { isNativeMobilePlatform } from '@core/mobile/isNativeMobilePlatform';
false && fileFolderDrop;
@@ -580,6 +584,12 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
)
.use(pinnedPropertiesPlugin());
+ if (isIOS || isNativeMobilePlatform()) {
+ plugins.use(
+ iosCursorScrollPlugin({ scrollContainer: () => md.scrollContainer })
+ );
+ }
+
if (ENABLE_MARKDOWN_LIVE_COLLABORATION) {
const getBlockLoroManager = blockLoroManagerSignal.get;
const peerId = () => getBlockLoroManager()?.getPeerIdStr();
@@ -991,7 +1001,7 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
</Show>
<ScopedPortal scope="block">
- <Show when={!isBlankMarkdown()}>
+ <Show when={!isBlankMarkdown() && !isMobile()}>
<div class="absolute bottom-2 left-2 w-fit h-fit">
<Wordcount stats={wordcountStats} />
</div>
@@ -1033,7 +1043,7 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
/>
</Show>
- <Show when={DEBUG}>
+ <Show when={DEBUG && !isMobile()}>
<Show when={state()}>
{(state) => (
<LexicalStateDebugger state={state()}></LexicalStateDebugger>
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/checklist/checklistPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/checklist/checklistPlugin.ts
index 5e20ebe27d..6319f2eea7 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/checklist/checklistPlugin.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/checklist/checklistPlugin.ts
@@ -32,7 +32,7 @@ function boundsCheck(rect: DOMRect, x: number, y: number): boolean {
x >= left - CLICK_BUFFER - oneRem * 1.25 &&
x <= left + CLICK_BUFFER &&
y >= top - CLICK_BUFFER &&
- y <= y + CLICK_BUFFER + oneRem
+ y <= top + CLICK_BUFFER + oneRem
) {
return true;
}
@@ -69,31 +69,31 @@ function wrapCheckboxMouseEvent(
function registerMouseEvents(editor: LexicalEditor) {
const mouseDownPosition = [0, 0];
- function click(e: MouseEvent) {
+ function click(e: PointerEvent) {
wrapCheckboxMouseEvent(e, editor, (e: MouseEvent) => {
- editor.update(() => {
- const { target, clientX, clientY } = e;
- if (!(target instanceof HTMLElement)) return;
+ const { target, clientX, clientY } = e;
+ if (!(target instanceof HTMLElement)) return;
+ if (!boundsCheck(target.getBoundingClientRect(), clientX, clientY))
+ return;
+ if (
+ Math.abs(mouseDownPosition[0] - clientX) >= 5 ||
+ Math.abs(mouseDownPosition[1] - clientY) >= 5
+ )
+ return;
+ editor.update(() => {
const nearest = $getNearestNodeFromDOMNode(target);
if ($isListItemNode(nearest)) {
- if (boundsCheck(target.getBoundingClientRect(), clientX, clientY)) {
- if (
- Math.abs(mouseDownPosition[0] - clientX) < 5 &&
- Math.abs(mouseDownPosition[1] - clientY) < 5
- ) {
- nearest.toggleChecked();
- }
- }
+ nearest.toggleChecked();
}
});
});
}
/**
- * Prevent default on checkbox mousedown to avoid focus change.
+ * Prevent default on checkbox pointerdown to avoid focus change.
*/
- function mousedown(e: MouseEvent) {
+ function pointerdown(e: PointerEvent) {
wrapCheckboxMouseEvent(e, editor, (e) => {
editor.read(() => {
const { target, clientX, clientY } = e;
@@ -113,11 +113,11 @@ function registerMouseEvents(editor: LexicalEditor) {
return editor.registerRootListener((root, prevRoot) => {
if (root) {
root.addEventListener('click', click);
- root.addEventListener('mousedown', mousedown);
+ root.addEventListener('pointerdown', pointerdown);
}
if (prevRoot) {
prevRoot.removeEventListener('click', click);
- prevRoot.removeEventListener('mousedown', mousedown);
+ prevRoot.removeEventListener('pointerdown', pointerdown);
}
});
}
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/index.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/index.ts
index 9e2af7acbf..cde5212be9 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/index.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/index.ts
@@ -13,6 +13,7 @@ export * from './file-paste';
export * from './find-and-replace';
export * from './generate';
export * from './horizontal-rules';
+export * from './ios-cursor-scroll';
export * from './insert-text';
export * from './katex';
export * from './keyboard-focus';
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/ios-cursor-scroll/index.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/ios-cursor-scroll/index.ts
new file mode 100644
index 0000000000..4f37662841
--- /dev/null
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/ios-cursor-scroll/index.ts
@@ -0,0 +1 @@
+export { iosCursorScrollPlugin } from './iosCursorScrollPlugin';
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/ios-cursor-scroll/iosCursorScrollPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/ios-cursor-scroll/iosCursorScrollPlugin.ts
new file mode 100644
index 0000000000..4d2dbb81e5
--- /dev/null
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/ios-cursor-scroll/iosCursorScrollPlugin.ts
@@ -0,0 +1,65 @@
+/**
+ * @file A plugin to ensure the cursor is visible above the iOS virtual keyboard.
+ * When the keyboard appears or the cursor moves into the safe zone near the
+ * keyboard, this scrolls the cursor into view.
+ */
+import { virtualKeyboardVisible } from '@core/mobile/virtualKeyboard';
+import type { Accessor } from 'solid-js';
+import type { LexicalEditor } from 'lexical';
+import { createEffect, createRoot, on } from 'solid-js';
+import { $getCaretRect } from '../../utils';
+
+const CURSOR_PADDING = 50; // ~2 lines above keyboard
+
+interface IosCursorScrollPluginOptions {
+ scrollContainer: Accessor<HTMLElement | undefined>;
+}
+
+export function iosCursorScrollPlugin(options: IosCursorScrollPluginOptions) {
+ return (editor: LexicalEditor) => {
+ let disposeRoot!: () => void;
+
+ const scrollCaretIntoView = () => {
+ const scrollEl = options.scrollContainer();
+ if (!scrollEl) return;
+
+ editor.read(() => {
+ const caretRect = $getCaretRect();
+ if (!caretRect) return;
+
+ const scrollRect = scrollEl.getBoundingClientRect();
+ const safeZoneBottom = scrollRect.bottom - CURSOR_PADDING;
+
+ if (caretRect.bottom > safeZoneBottom) {
+ const scrollAmount = caretRect.bottom - safeZoneBottom;
+ // For some reason, setting this to 'instant' makes it scroll to the wrong location???
+ scrollEl.scrollBy({ top: scrollAmount, behavior: 'smooth' });
+ }
+ });
+ };
+
+ let rafId = 0;
+ const removeUpdateListener = editor.registerUpdateListener(() => {
+ if (!virtualKeyboardVisible()) return;
+ cancelAnimationFrame(rafId);
+ rafId = requestAnimationFrame(scrollCaretIntoView);
+ });
+
+ createRoot((dispose) => {
+ disposeRoot = dispose;
+
+ createEffect(
+ on(virtualKeyboardVisible, (visible) => {
+ if (!visible) return;
+ setTimeout(scrollCaretIntoView, 0);
+ })
+ );
+ });
+
+ return () => {
+ cancelAnimationFrame(rafId);
+ removeUpdateListener();
+ disposeRoot();
+ };
+ };
+}
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/restoreFocusPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/restoreFocusPlugin.ts
index 6af49ba086..6e827ff292 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/restoreFocusPlugin.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/restore-focus/restoreFocusPlugin.ts
@@ -14,11 +14,17 @@ export function restoreFocusPlugin() {
let clickFlag = false;
return (editor: LexicalEditor) => {
return mergeRegister(
- registerRootEventListener(editor, 'pointerdown', () => {
+ registerRootEventListener(editor, 'pointerdown', (e: PointerEvent) => {
clickFlag = true;
- setTimeout(() => {
- clickFlag = false;
- });
+
+ setTimeout(
+ () => {
+ clickFlag = false;
+ // On click, focusin happens synchonously after pointerdown, with the setTimeout flipping the flag back after. This is deterministic and good.
+ // On iOS touch these do not happen synchronously, so we're blindly flipping the flag back after 500ms.
+ },
+ e.pointerType === 'touch' ? 500 : 0
+ );
}),
registerRootEventListener(editor, 'focusin', (e) => {
if (clickFlag) return;