1- import type { MessagesFromBackground } from "../ghosttext-runner/message"
1+ import type { ExternalEdit , MessagesFromBackground } from "../ghosttext-runner"
2+ import type { BodyState } from "../ghosttext-session/types"
23import type { IBackgroundMessenger , IGhostClientPort } from "./api"
34
45export class ComposeEventRouter {
56 static isSingleton = true
67
7- constructor ( readonly backgroundMessenger : IBackgroundMessenger ) { }
8+ constructor (
9+ readonly body : HTMLBodyElement ,
10+ readonly backgroundMessenger : IBackgroundMessenger ,
11+ ) { }
812
913 async handleConnect ( port : IGhostClientPort ) : Promise < void > {
10- const body = document . body
11- const clearReadOnly = makeReadOnly ( body )
14+ const clearReadOnly = makeReadOnly ( this . body )
1215 try {
1316 console . info ( { connected : Date . now ( ) , date : new Date ( ) } )
17+
18+ port . send ( { html : this . body . innerHTML , plainText : this . body . textContent } satisfies BodyState )
19+
1420 for ( ; ; ) {
15- let ready = await port . waitReady ( ) . then (
16- ( ) => true ,
17- ( ) => false ,
18- )
19- if ( ! ready ) {
20- break
21- }
21+ await port . waitReady ( )
22+
2223 let got = port . clearReceived ( )
23- console . debug ( { got } )
24- if ( ! got ?. text ) {
24+
25+ if ( ! got || ! this . applyEdit ( got ) ) {
2526 break
2627 }
28+
2729 // TODO sync cursors
28- body . textContent = got . text
2930 // TODO send changes
30- // TODO reconnect
3131 }
32+ } catch ( thrown ) {
33+ console . info ( { thrown } )
3234 } finally {
3335 console . info ( { disconnected : Date . now ( ) , date : new Date ( ) } )
3436 clearReadOnly ( )
@@ -40,16 +42,26 @@ export class ComposeEventRouter {
4042
4143 if ( message === "ping" ) {
4244 return response ( message )
43- } else if ( message === "start" ) {
44- return "ok"
45- } else if ( message === "stop" ) {
46- return "ok"
47- } else if ( message === "toggle" ) {
48- return "ok"
4945 } else {
5046 return "ok"
5147 }
5248 }
49+
50+ private applyEdit ( got : ExternalEdit ) : boolean {
51+ console . debug ( { got } )
52+
53+ if ( typeof got . plainText === "string" ) {
54+ this . body . textContent = got . plainText
55+ return true
56+ }
57+
58+ if ( typeof got . html === "string" ) {
59+ this . body . innerHTML = got . html
60+ return true
61+ }
62+
63+ return false
64+ }
5365}
5466
5567function response ( _message : "ping" ) : MessagesFromBackground [ "ping" ] {
@@ -67,15 +79,18 @@ function makeReadOnly(body: HTMLElement): () => void {
6779 const controller = new AbortController ( )
6880 const option = { signal : controller . signal }
6981
70- body . style . background = "slategray"
71-
7282 body . addEventListener ( "beforeinput" , disable , option )
7383 body . addEventListener ( "paste" , disable , option )
7484 body . addEventListener ( "cut" , disable , option )
7585 body . addEventListener ( "drop" , disable , option )
7686 body . addEventListener ( "dragover" , disable , option )
7787
88+ body . style . background = "lightgray"
89+ body . style . pointerEvents = "none"
90+ body . blur ( )
91+
7892 return ( ) => {
93+ body . style . removeProperty ( "pointerEvents" )
7994 body . style . removeProperty ( "background" )
8095 controller . abort ( )
8196 }
0 commit comments