-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #750 from streamich/peritext-inline-events
Peritext rendering surface improvements
- Loading branch information
Showing
30 changed files
with
490 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type {PeritextEventTarget} from '../events/PeritextEventTarget'; | ||
import type {UiLifeCycles} from './types'; | ||
import type {Peritext} from '../../json-crdt-extensions/peritext'; | ||
import type {Printable} from 'tree-dump'; | ||
|
||
export interface CompositionControllerOpts { | ||
source: HTMLElement; | ||
txt: Peritext; | ||
et: PeritextEventTarget; | ||
} | ||
|
||
export class CompositionController implements UiLifeCycles, Printable { | ||
public composing = false; | ||
public data: string = ''; | ||
|
||
public constructor(public readonly opts: CompositionControllerOpts) {} | ||
|
||
/** -------------------------------------------------- {@link UiLifeCycles} */ | ||
|
||
public start(): void { | ||
const el = this.opts.source; | ||
el.addEventListener('compositionstart', this.onStart); | ||
el.addEventListener('compositionupdate', this.onUpdate); | ||
el.addEventListener('compositionend', this.onEnd); | ||
} | ||
|
||
public stop(): void { | ||
const el = this.opts.source; | ||
el.removeEventListener('compositionstart', this.onStart); | ||
el.removeEventListener('compositionupdate', this.onUpdate); | ||
el.removeEventListener('compositionend', this.onEnd); | ||
} | ||
|
||
private onStart = (event: CompositionEvent): void => { | ||
this.composing = true; | ||
this.data = event.data; | ||
}; | ||
|
||
private onUpdate = (event: CompositionEvent): void => { | ||
this.composing = true; | ||
this.data = event.data; | ||
}; | ||
|
||
private onEnd = (event: CompositionEvent): void => { | ||
this.composing = false; | ||
this.data = ''; | ||
const text = event.data; | ||
if (text) this.opts.et.insert(text); | ||
}; | ||
|
||
/** ----------------------------------------------------- {@link Printable} */ | ||
|
||
public toString(tab?: string): string { | ||
return `composition { composing: ${this.composing}, data: ${JSON.stringify(this.data)} }`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {printTree, type Printable} from 'tree-dump'; | ||
import {InputController} from '../dom/InputController'; | ||
import {CursorController} from '../dom/CursorController'; | ||
import {RichTextController} from '../dom/RichTextController'; | ||
import {PeritextEventDefaults} from '../events/PeritextEventDefaults'; | ||
import {PeritextEventTarget} from '../events/PeritextEventTarget'; | ||
import {KeyController} from '../dom/KeyController'; | ||
import {CompositionController} from '../dom/CompositionController'; | ||
import type {UiLifeCycles} from '../dom/types'; | ||
import type {Peritext} from '../../json-crdt-extensions'; | ||
|
||
export interface DomControllerOpts { | ||
source: HTMLElement; | ||
txt: Peritext; | ||
} | ||
|
||
export class DomController implements UiLifeCycles, Printable { | ||
public readonly et: PeritextEventTarget; | ||
public readonly keys: KeyController; | ||
public readonly comp: CompositionController; | ||
public readonly input: InputController; | ||
public readonly cursor: CursorController; | ||
public readonly richText: RichTextController; | ||
|
||
constructor(public readonly opts: DomControllerOpts) { | ||
const {source, txt} = opts; | ||
const et = (this.et = new PeritextEventTarget()); | ||
const defaults = new PeritextEventDefaults(txt, et); | ||
et.defaults = defaults; | ||
const keys = (this.keys = new KeyController()); | ||
const comp = (this.comp = new CompositionController({et, source, txt})); | ||
this.input = new InputController({et, source, txt, comp}); | ||
this.cursor = new CursorController({et, source, txt, keys}); | ||
this.richText = new RichTextController({et, source, txt}); | ||
} | ||
|
||
/** -------------------------------------------------- {@link UiLifeCycles} */ | ||
|
||
public start(): void { | ||
this.keys.start(); | ||
this.comp.start(); | ||
this.input.start(); | ||
this.cursor.start(); | ||
this.richText.start(); | ||
} | ||
|
||
public stop(): void { | ||
this.keys.stop(); | ||
this.comp.stop(); | ||
this.input.stop(); | ||
this.cursor.stop(); | ||
this.richText.stop(); | ||
} | ||
|
||
/** ----------------------------------------------------- {@link Printable} */ | ||
|
||
public toString(tab?: string): string { | ||
return ( | ||
'DOM' + | ||
printTree(tab, [ | ||
(tab) => this.cursor.toString(tab), | ||
(tab) => this.keys.toString(tab), | ||
(tab) => this.comp.toString(tab), | ||
]) | ||
); | ||
} | ||
} |
Oops, something went wrong.