Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .playwright/tests/checkboxList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,33 @@ test.describe('checkbox list (web)', () => {
const html = await getSerializedHtml(page);
expect(html.match(/<li checked/g)?.length).toBe(1);
});

test('clicking checkbox moves cursor', async ({ page }) => {
await setEditorHtml(
page,
'<html><ul data-type="checkbox"><li>one</li><li>two</li></ul></html>'
);

const editor = editorLocator(page);
const checkbox = editor.locator('input[type="checkbox"]').first();
await checkbox.click();

const selection = await editor.evaluate(() => {
const s = window.getSelection();
if (!s || s.rangeCount === 0) {
return null;
}
const range = s.getRangeAt(0);
return {
startOffset: range.startOffset,
endOffset: range.endOffset,
collapsed: range.collapsed,
};
});
expect(selection).toEqual({
startOffset: 3,
endOffset: 3,
collapsed: true,
});
});
});
23 changes: 9 additions & 14 deletions src/web/EnrichedTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ import {
nativePosToTiptapPos,
nativeLeafText,
} from './nativeMappers/positionMapping';
import {
useEditor,
EditorContent,
type ChainedCommands,
Editor,
} from '@tiptap/react';
import { useEditor, EditorContent, Editor } from '@tiptap/react';
import Document from '@tiptap/extension-document';
import Paragraph from '@tiptap/extension-paragraph';
import Text from '@tiptap/extension-text';
Expand Down Expand Up @@ -92,13 +87,8 @@ import {
import { assertBrowserEnvironment } from './utils/assertBrowserEnvironment';
import { runSafelyInEditor } from './utils/runSafelyInEditor';
import { useLinkPress } from './htmlExtensions/useLinkPress';

function runFocused(
editor: Editor,
apply: (chain: ChainedCommands) => ChainedCommands
) {
runSafelyInEditor(editor, (e) => apply(e.chain().focus()).run());
}
import { handleCheckboxPress } from './htmlExtensions/handleCheckboxPress';
import { runFocused } from './utils/runFocused';

export const EnrichedTextInput = ({
ref,
Expand Down Expand Up @@ -292,7 +282,12 @@ export const EnrichedTextInput = ({
editorProps: {
handleKeyDown: (view, event) => handleKeyDown(view.state.doc, event),
handleDOMEvents: {
click: (_view, event) => handleLinkPress(event),
click: (view, event) => {
if (handleCheckboxPress(event, editor, view)) {
return true;
}
return handleLinkPress(event);
},
mousedown: (_view, event) => handleLinkMouseDown(event),
},
handlePaste: (_view, event) =>
Expand Down
19 changes: 19 additions & 0 deletions src/web/htmlExtensions/handleCheckboxPress.ts
Comment thread
relextm19 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Editor } from '@tiptap/core';
import type { EditorView } from '@tiptap/pm/view';
import { runFocused } from '../utils/runFocused';

export function handleCheckboxPress(
event: MouseEvent,
editor: Editor,
view: EditorView
): boolean {
const target = event.target;
if (target instanceof HTMLInputElement && target.type === 'checkbox') {
const position = view.posAtDOM(target, 0);
const node = view.state.doc.nodeAt(position);
if (!node) return false;
runFocused(editor, (c) => c.setTextSelection(position + node.nodeSize - 1));
return true;
}
return false;
}
8 changes: 8 additions & 0 deletions src/web/utils/runFocused.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Editor, type ChainedCommands } from '@tiptap/core';
import { runSafelyInEditor } from './runSafelyInEditor';
export function runFocused(
editor: Editor,
apply: (chain: ChainedCommands) => ChainedCommands
) {
runSafelyInEditor(editor, (e) => apply(e.chain().focus()).run());
}
Loading