Skip to content

Peritext UI events location #899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SubscriptionEventTarget} from '../../util/events/TypedEventTarget';
import {SubscriptionEventTarget} from '../../../util/events/TypedEventTarget';
import type {
PeritextEventDetailMap,
CursorDetail,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {CursorAnchor} from '../../../json-crdt-extensions';
import {type Kit, runAlphabetKitTestSuite} from '../../../json-crdt-extensions/peritext/__tests__/setup';
import {CursorAnchor} from '../../../../json-crdt-extensions';
import {type Kit, runAlphabetKitTestSuite} from '../../../../json-crdt-extensions/peritext/__tests__/setup';
import {PeritextEventDefaults} from '../defaults/PeritextEventDefaults';
import {PeritextEventTarget} from '../PeritextEventTarget';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type Kit, runAlphabetKitTestSuite} from '../../../json-crdt-extensions/peritext/__tests__/setup';
import {type Kit, runAlphabetKitTestSuite} from '../../../../json-crdt-extensions/peritext/__tests__/setup';
import {PeritextEventDefaults} from '../defaults/PeritextEventDefaults';
import {PeritextEventTarget} from '../PeritextEventTarget';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type Kit, runAlphabetKitTestSuite} from '../../../json-crdt-extensions/peritext/__tests__/setup';
import {type Kit, runAlphabetKitTestSuite} from '../../../../json-crdt-extensions/peritext/__tests__/setup';
import {PeritextEventDefaults} from '../defaults/PeritextEventDefaults';
import {PeritextEventTarget} from '../PeritextEventTarget';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type Kit, runAlphabetKitTestSuite} from '../../../json-crdt-extensions/peritext/__tests__/setup';
import {type Kit, runAlphabetKitTestSuite} from '../../../../json-crdt-extensions/peritext/__tests__/setup';
import {PeritextEventDefaults} from '../defaults/PeritextEventDefaults';
import {PeritextEventTarget} from '../PeritextEventTarget';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {createEvents} from '..';
import {type Kit, runAlphabetKitTestSuite} from '../../../json-crdt-extensions/peritext/__tests__/setup';
import {SliceTypeCon} from '../../../json-crdt-extensions/peritext/slice/constants';
import {create as createTransfer} from '../../../json-crdt-extensions/peritext/transfer/create';
import {type Kit, runAlphabetKitTestSuite} from '../../../../json-crdt-extensions/peritext/__tests__/setup';
import {SliceTypeCon} from '../../../../json-crdt-extensions/peritext/slice/constants';
import {create as createTransfer} from '../../../../json-crdt-extensions/peritext/transfer/create';

const testSuite = (getKit: () => Kit) => {
const setup = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {saveSelection} from '../../web/util';
import {saveSelection} from '../../../../util/dom';
import type {PeritextClipboard, PeritextClipboardData} from './types';

const toText = (buf: Uint8Array) => new TextDecoder().decode(buf);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {Anchor} from '../../../json-crdt-extensions/peritext/rga/constants';
import {Anchor} from '../../../../json-crdt-extensions/peritext/rga/constants';
import {placeCursor} from './annals';
import {Cursor} from '../../../json-crdt-extensions/peritext/editor/Cursor';
import {CursorAnchor, type Peritext} from '../../../json-crdt-extensions/peritext';
import {PersistedSlice} from '../../../json-crdt-extensions/peritext/slice/PersistedSlice';
import type {Range} from '../../../json-crdt-extensions/peritext/rga/Range';
import type {PeritextDataTransfer} from '../../../json-crdt-extensions/peritext/transfer/PeritextDataTransfer';
import {Cursor} from '../../../../json-crdt-extensions/peritext/editor/Cursor';
import {CursorAnchor, type Peritext} from '../../../../json-crdt-extensions/peritext';
import {PersistedSlice} from '../../../../json-crdt-extensions/peritext/slice/PersistedSlice';
import type {Range} from '../../../../json-crdt-extensions/peritext/rga/Range';
import type {PeritextDataTransfer} from '../../../../json-crdt-extensions/peritext/transfer/PeritextDataTransfer';
import type {PeritextEventHandlerMap, PeritextEventTarget} from '../PeritextEventTarget';
import type {EditorSlices} from '../../../json-crdt-extensions/peritext/editor/EditorSlices';
import type {EditorSlices} from '../../../../json-crdt-extensions/peritext/editor/EditorSlices';
import type * as events from '../types';
import type {PeritextClipboard, PeritextClipboardData} from '../clipboard/types';
import type {UndoCollector} from '../../types';
import type {UndoCollector} from './types';
import type {UiHandle} from './ui/UiHandle';
import type {Point} from '../../../json-crdt-extensions/peritext/rga/Point';
import type {EditorUi} from '../../../json-crdt-extensions/peritext/editor/types';
import type {Point} from '../../../../json-crdt-extensions/peritext/rga/Point';
import type {EditorUi} from '../../../../json-crdt-extensions/peritext/editor/types';

const toText = (buf: Uint8Array) => new TextDecoder().decode(buf);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {Peritext} from '../../../json-crdt-extensions';
import {Anchor} from '../../../json-crdt-extensions/peritext/rga/constants';
import {DelOp, equal, InsArrOp, InsBinOp, InsStrOp, type Patch, Timestamp} from '../../../json-crdt-patch';
import type {Range} from '../../../json-crdt-extensions/peritext/rga/Range';
import {Anchor} from '../../../../json-crdt-extensions/peritext/rga/constants';
import {DelOp, equal, InsArrOp, InsBinOp, InsStrOp, type Patch, Timestamp} from '../../../../json-crdt-patch';
import type {Peritext} from '../../../../json-crdt-extensions';
import type {Range} from '../../../../json-crdt-extensions/peritext/rga/Range';

/**
* Given an undo/redo patch/batch, calculates a good cursor position to place
Expand Down
7 changes: 7 additions & 0 deletions src/json-crdt-extensions/peritext/events/defaults/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface UndoCollector {
/**
* Mark the currently minted change {@link Patch} in {@link Builder} for undo.
* It will be picked up during the next flush.
*/
capture(): void;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {tick} from '../../../../json-crdt-patch';
import {Anchor} from '../../../../json-crdt-extensions/peritext/rga/constants';
import type {Peritext} from '../../../../json-crdt-extensions';
import type {Point} from '../../../../json-crdt-extensions/peritext/rga/Point';
import type {Rect} from '../../../web/types';
import type {PeritextUiApi, UiLineEdge, UiLineInfo} from './types';
import {Anchor} from '../../../../../json-crdt-extensions/peritext/rga/constants';
import type {Peritext} from '../../../../../json-crdt-extensions';
import type {Point} from '../../../../../json-crdt-extensions/peritext/rga/Point';
import type {PeritextUiApi, Rect, UiLineEdge, UiLineInfo} from './types';

export class UiHandle {
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type {Point} from '../../../../json-crdt-extensions/peritext/rga/Point';
import type {ITimestampStruct} from '../../../../json-crdt-patch';
import type {Rect} from '../../../web/types';
import type {Point} from '../../../../../json-crdt-extensions/peritext/rga/Point';
import type {ITimestampStruct} from '../../../../../json-crdt-patch';

export interface Rect {
x: number;
y: number;
width: number;
height: number;
}

Comment on lines +3 to 10
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining a Rect interface here duplicates types likely defined elsewhere (e.g., in web/types); consider reusing or aliasing a shared Rect type to prevent divergence.

Suggested change
export interface Rect {
x: number;
y: number;
width: number;
height: number;
}
import type { Rect } from '../../../../../web/types';

Copilot uses AI. Check for mistakes.

/**
* API which a rendering surface (UI) must implement to be used by the event
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {PeritextEventDefaults, type PeritextEventDefaultsOpts} from './defaults/PeritextEventDefaults';
import {PeritextEventTarget} from './PeritextEventTarget';
import {DomClipboard} from './clipboard/DomClipboard';
import {create as createDataTransfer} from '../../json-crdt-extensions/peritext/transfer/create';
import type {Peritext} from '../../json-crdt-extensions';
import {create as createDataTransfer} from '../../../json-crdt-extensions/peritext/transfer/create';
import type {Peritext} from '../../../json-crdt-extensions';

/**
* @todo Move into separately importable file.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type {Point} from '../../json-crdt-extensions/peritext/rga/Point';
import type {EditorPosition, EditorSelection} from '../../json-crdt-extensions/peritext/editor/types';
import type {SliceType} from '../../json-crdt-extensions/peritext/slice/types';
import type {Patch} from '../../json-crdt-patch';
import type {Cursor} from '../../json-crdt-extensions/peritext/editor/Cursor';
import type {Range} from '../../json-crdt-extensions/peritext/rga/Range';
import type {Point} from '../../../json-crdt-extensions/peritext/rga/Point';
import type {EditorPosition, EditorSelection} from '../../../json-crdt-extensions/peritext/editor/types';
import type {SliceType} from '../../../json-crdt-extensions/peritext/slice/types';
import type {Patch} from '../../../json-crdt-patch';
import type {Cursor} from '../../../json-crdt-extensions/peritext/editor/Cursor';
import type {Range} from '../../../json-crdt-extensions/peritext/rga/Range';

/**
* Dispatched every time any other event is dispatched.
Expand Down
2 changes: 1 addition & 1 deletion src/json-crdt-peritext-ui/plugins/cursor/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ValueSyncStore} from '../../../util/events/sync-store';
import type {ChangeDetail} from '../../events/types';
import type {ChangeDetail} from '../../../json-crdt-extensions/peritext/events';
import type {PeritextSurfaceState, UiLifeCycles} from '../../web';

export class CursorState implements UiLifeCycles {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as React from 'react';
import {Chrome} from './Chrome';
import {context, type MinimalPluginContextValue} from './context';
import {ValueSyncStore} from '../../../util/events/sync-store';
import type {ChangeDetail} from '../../events/types';
import type {PeritextSurfaceState} from '../../web';
import type {ChangeDetail} from '../../../json-crdt-extensions/peritext/events';

export interface RenderPeritextProps {
ctx: PeritextSurfaceState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';
import {FormattingsManagePane} from '../../../formatting/FormattingsManagePane';
import {BottomPanePortal} from '../../util/BottomPanePortal';
import {useToolbarPlugin} from '../../../context';
import type {PeritextEventDetailMap} from '../../../../../events';
import type {PeritextEventDetailMap} from '../../../../../../json-crdt-extensions/peritext/events';
Copy link
Preview

Copilot AI May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] This long relative import path can be brittle and hard to maintain; consider configuring TypeScript path aliases to simplify and shorten imports.

Suggested change
import type {PeritextEventDetailMap} from '../../../../../../json-crdt-extensions/peritext/events';
import type {PeritextEventDetailMap} from 'json-crdt-extensions/peritext/events';

Copilot uses AI. Check for mistakes.

import type {CaretViewProps} from '../../../../../web/react/cursor/CaretView';

const isDirectCaretPlacement = (event: PeritextEventDetailMap['change']['ev'] | undefined): boolean => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import {SliceTypeCon} from '../../../../json-crdt-extensions/peritext/slice/cons
import {NewFormatting} from './formattings';
import * as behavior from '../formatting/tags';
import type {UiLifeCycles} from '../../../web/types';
import type {BufferDetail, PeritextCursorEvent, PeritextEventDetailMap} from '../../../events/types';
import type {PeritextSurfaceState} from '../../../web';
import type {MenuItem} from '../types';
import type {ToolbarPluginOpts} from '../ToolbarPlugin';
import type {
BufferDetail,
PeritextCursorEvent,
PeritextEventDetailMap,
} from '../../../../json-crdt-extensions/peritext/events';

export class ToolbarState implements UiLifeCycles {
public readonly txt: Peritext;
Expand Down
8 changes: 0 additions & 8 deletions src/json-crdt-peritext-ui/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
export interface UndoCollector {
/**
* Mark the currently minted change {@link Patch} in {@link Builder} for undo.
* It will be picked up during the next flush.
*/
capture(): void;
}

export interface UndoManager {
push<U, R>(undo: UndoItem<U, R>): void;
undo(): void;
Expand Down
2 changes: 1 addition & 1 deletion src/json-crdt-peritext-ui/web/dom/CursorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import {ElementAttr} from '../constants';
import {throttle} from '../../../util/throttle';
import {ValueSyncStore} from '../../../util/events/sync-store';
import type {Printable} from 'tree-dump';
import type {PeritextEventTarget} from '../../events/PeritextEventTarget';
import type {UiLifeCycles} from '../types';
import type {Inline} from '../../../json-crdt-extensions/peritext/block/Inline';
import type {DomController} from './DomController';
import type {PeritextEventTarget} from '../../../json-crdt-extensions/peritext/events/PeritextEventTarget';

/**
* Controller for handling text selection and cursor movements. Listens to
Expand Down
8 changes: 4 additions & 4 deletions src/json-crdt-peritext-ui/web/dom/DomController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import {CompositionController} from './CompositionController';
import {AnnalsController} from './annals/AnnalsController';
import {ElementAttr} from '../constants';
import {Anchor} from '../../../json-crdt-extensions/peritext/rga/constants';
import {UiHandle} from '../../events/defaults/ui/UiHandle';
import {compare, type ITimestampStruct} from '../../../json-crdt-patch';
import {UiHandle} from '../../../json-crdt-extensions/peritext/events/defaults/ui/UiHandle';
import type {Point} from '../../../json-crdt-extensions/peritext/rga/Point';
import type {PeritextEventDefaults} from '../../events/defaults/PeritextEventDefaults';
import type {Log} from '../../../json-crdt/log/Log';
import type {PeritextEventTarget} from '../../events/PeritextEventTarget';
import type {Rect, UiLifeCycles} from '../types';
import type {Inline, Peritext} from '../../../json-crdt-extensions';
import type {Range} from '../../../json-crdt-extensions/peritext/rga/Range';
import type {PeritextUiApi} from '../../events/defaults/ui/types';
import type {PeritextEventTarget} from '../../../json-crdt-extensions/peritext/events/PeritextEventTarget';
import type {PeritextUiApi} from '../../../json-crdt-extensions/peritext/events/defaults/ui/types';
import type {PeritextEventDefaults} from '../../../json-crdt-extensions/peritext/events/defaults/PeritextEventDefaults';

export class DomController implements UiLifeCycles, Printable, PeritextUiApi {
public readonly txt: Peritext;
Expand Down
2 changes: 1 addition & 1 deletion src/json-crdt-peritext-ui/web/dom/InputController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {unit} from '../util';
import {SliceTypeCon} from '../../../json-crdt-extensions/peritext/slice/constants';
import type {Peritext} from '../../../json-crdt-extensions/peritext';
import type {PeritextEventTarget} from '../../events/PeritextEventTarget';
import type {PeritextEventTarget} from '../../../json-crdt-extensions/peritext/events/PeritextEventTarget';
import type {TypedEventTarget} from '../../../util/events/TypedEventTarget';
import type {UiLifeCycles} from '../types';
import type {DomController} from './DomController';
Expand Down
5 changes: 3 additions & 2 deletions src/json-crdt-peritext-ui/web/dom/annals/AnnalsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {printTree, type Printable} from 'tree-dump';
import type {Patch} from '../../../../json-crdt-patch';
import type {Peritext} from '../../../../json-crdt-extensions';
import type {UiLifeCycles} from '../../types';
import type {RedoCallback, RedoItem, UndoCallback, UndoCollector, UndoItem} from '../../../types';
import type {RedoCallback, RedoItem, UndoCallback, UndoItem} from '../../../types';
import type {Log} from '../../../../json-crdt/log/Log';
import type {PeritextEventTarget} from '../../../events/PeritextEventTarget';
import type {UndoCollector} from '../../../../json-crdt-extensions/peritext/events/defaults/types';
import type {PeritextEventTarget} from '../../../../json-crdt-extensions/peritext/events/PeritextEventTarget';

export interface UndoRedoControllerOpts {
log: Log;
Expand Down
2 changes: 1 addition & 1 deletion src/json-crdt-peritext-ui/web/dom/annals/WebUndo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {saveSelection} from '../../util';
import {saveSelection} from '../../../../util/dom';
import type {UndoManager, UndoItem} from '../../../types';
import type {UiLifeCycles} from '../../types';

Expand Down
3 changes: 2 additions & 1 deletion src/json-crdt-peritext-ui/web/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './react';
export * from './types';
export {PeritextSurfaceState} from './state';
export {UiLifeCycles, Rect} from './types';
export {UiLifeCycles} from './types';
2 changes: 1 addition & 1 deletion src/json-crdt-peritext-ui/web/react/PeritextView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {CssClass} from '../constants';
import {CursorPlugin} from '../../plugins/cursor';
import {defaultPlugin} from '../../plugins/minimal';
import {PeritextSurfaceState} from '../state';
import {createEvents} from '../../events';
import {context} from './context';
import {BlockView} from './BlockView';
import {useBehaviorSubject} from 'nice-ui/lib/hooks/useBehaviorSubject';
import {createEvents} from '../../../json-crdt-extensions/peritext/events';
import type {PeritextPlugin} from './types';
import type {Peritext} from '../../../json-crdt-extensions';

Expand Down
2 changes: 1 addition & 1 deletion src/json-crdt-peritext-ui/web/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Log} from '../../../json-crdt/log/Log';
import {DomController} from '../dom/DomController';
import type {PeritextPlugin} from '../react/types';
import type {Peritext} from '../../../json-crdt-extensions/peritext/Peritext';
import type {PeritextEventDefaults} from '../../events/defaults/PeritextEventDefaults';
import type {PeritextEventDefaults} from '../../../json-crdt-extensions/peritext/events/defaults/PeritextEventDefaults';
import type {UiLifeCycles} from '../types';

export class PeritextSurfaceState implements UiLifeCycles {
Expand Down
2 changes: 1 addition & 1 deletion src/json-crdt-peritext-ui/web/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export interface UiLifeCycles {
start(): () => void;
}

export type Rect = Pick<DOMRect, 'x' | 'y' | 'width' | 'height'>;
export {Rect} from '../../json-crdt-extensions/peritext/events/defaults/ui/types';
17 changes: 0 additions & 17 deletions src/json-crdt-peritext-ui/web/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@ export const getCursorPosition: GetCursorPosition =
export const unit = (event: KeyboardEvent): '' | 'word' | 'vline' =>
event.metaKey ? 'vline' : event.altKey || event.ctrlKey ? 'word' : '';

/**
* Save the current browser selection, so that it can be restored later. Returns
* a callback to restore the selection.
*
* @returns Callback to restore the selection.
*/
export const saveSelection = (): (() => void) | undefined => {
const selection = window?.getSelection();
if (!selection) return;
const ranges: Range[] = [];
for (let i = 0; i < selection.rangeCount; i++) ranges.push(selection.getRangeAt(i));
return () => {
selection.removeAllRanges();
for (const range of ranges) selection.addRange(range);
};
};

export const getDomain = (url: string): string | undefined =>
url.match(/^(?:[^:\/]+:)?(?:\/{1,5})?(([^\/$ \.]+)\.([^\/$ ]+))/i)?.[1];

Expand Down
16 changes: 16 additions & 0 deletions src/util/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Save the current browser selection, so that it can be restored later. Returns
* a callback to restore the selection.
*
* @returns Callback to restore the selection.
*/
export const saveSelection = (): (() => void) | undefined => {
const selection = window?.getSelection();
if (!selection) return;
const ranges: Range[] = [];
for (let i = 0; i < selection.rangeCount; i++) ranges.push(selection.getRangeAt(i));
return () => {
selection.removeAllRanges();
for (const range of ranges) selection.addRange(range);
};
};