Skip to content

Commit c1f0ac3

Browse files
committed
single dispatch system for doc and tab
1 parent 19a802f commit c1f0ac3

1 file changed

Lines changed: 54 additions & 85 deletions

File tree

modules/writing_observer/writing_observer/reconstruct_doc.py

Lines changed: 54 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,12 @@ def command_list(doc, commands):
189189
loading the history of a new doc, or in updating a document from
190190
new `save` requests.
191191
'''
192+
doc_state = DocState(user_id="", doc_id="")
193+
tab = doc_state.tabs["t.0"]
194+
tab.doc = doc
192195
for item in commands:
193-
if item['ty'] in text_dispatch:
194-
doc = text_dispatch[item['ty']](doc, **item)
195-
else:
196-
print("Unrecogized Google Docs command: " + repr(item['ty']))
197-
# TODO: Log issue and fix it!
198-
return doc
196+
doc_state._dispatch_cmd(item, "t.0", None)
197+
return tab.doc
199198

200199

201200
def multi(doc, mts, ty):
@@ -327,56 +326,6 @@ def null(doc, **kwargs):
327326
return doc
328327

329328

330-
# This dictionary maps the `ty` parameter to the function which
331-
# handles text edits.
332-
333-
# TODO: `ae,``ue,` `de,` and `te` need to be
334-
# reverse-engineered. These happens if we e.g. make a new bullet
335-
# list, or add an image.
336-
337-
# TODO: 'iss' and 'dss' are generated when suggested text is inserted or deleted.
338-
# these can't be handled like plain 'is' or 'ds' because the include different fields
339-
# (e.g., 'sugid', presumably, suggestion id.)
340-
text_dispatch = {
341-
'ae': null,
342-
'ase': null, # suggestion
343-
'ast': null, # suggestion. Image?
344-
'astss': null, # suggestion. Autospell?
345-
'ue': null,
346-
'de': null,
347-
'dse': null, # suggestion
348-
'dss': null, # suggested deletion
349-
'te': null,
350-
'as': alter,
351-
'ds': delete,
352-
'is': insert,
353-
'iss': null, # suggested insertion
354-
'mefd': null, # suggestion
355-
'mlti': multi,
356-
'msfd': null, # suggestion
357-
'null': null,
358-
'ord': null,
359-
'ras': null, # suggestion. Autospell?
360-
'rplc': replace, # rplc is called as the first edit
361-
# when the document is created from
362-
# a template, so if you want to know
363-
# what text was NOT written by the author,
364-
# logging the text buffer after the initial
365-
# rplc action will give you that.
366-
'rte': null, # suggestion
367-
'rue': null, # suggestion
368-
'rvrt': replace, # apparently logged after an undo
369-
'sas': null, # suggestion. Autospell?
370-
'sl': null,
371-
'ste': null, # suggestion
372-
'sue': null, # suggestion
373-
'uefd': null, # suggestion
374-
'use': null, # suggestion
375-
'umv': null,
376-
'usfd': null, # suggestion
377-
}
378-
379-
380329
def _touch_tab(tab, event_timestamp):
381330
if event_timestamp is None:
382331
return
@@ -396,10 +345,26 @@ def tab(self) -> "TabState":
396345
return self.doc_state.tabs[self.current_tab]
397346

398347

399-
def _cmd_text(ctx: CommandContext, **cmd):
400-
ty = cmd.get("ty")
401-
if ty in text_dispatch:
402-
text_dispatch[ty](ctx.tab.doc, **cmd)
348+
def _cmd_is(ctx: CommandContext, ty=None, ibi=None, s=None, **kwargs):
349+
if ibi is None or s is None:
350+
return
351+
insert(ctx.tab.doc, ty, ibi, s)
352+
353+
354+
def _cmd_ds(ctx: CommandContext, ty=None, si=None, ei=None, **kwargs):
355+
if si is None or ei is None:
356+
return
357+
delete(ctx.tab.doc, ty, si, ei)
358+
359+
360+
def _cmd_as(ctx: CommandContext, **cmd):
361+
alter(ctx.tab.doc, **cmd)
362+
363+
364+
def _cmd_rplc(ctx: CommandContext, ty=None, snapshot=None, **kwargs):
365+
if snapshot is None:
366+
return
367+
replace(ctx.tab.doc, ty, snapshot)
403368

404369

405370
def _cmd_mlti(ctx: CommandContext, mts=None, **kwargs):
@@ -473,6 +438,10 @@ def _cmd_null(ctx: CommandContext, **kwargs):
473438

474439

475440
# Centralized dispatch for all command types.
441+
#
442+
# Notes:
443+
# - Some commands are formatting-only or suggested edits and are treated as no-ops.
444+
# - Dropdown and element handling is routed through tab-aware handlers.
476445
dispatch = {
477446
'mlti': _cmd_mlti,
478447
'nm': _cmd_nm,
@@ -481,34 +450,34 @@ def _cmd_null(ctx: CommandContext, **kwargs):
481450
'ac': _cmd_ac,
482451
'ae': _cmd_ae,
483452
'te': _cmd_te,
484-
'as': _cmd_text,
485-
'ds': _cmd_text,
486-
'is': _cmd_text,
487-
'iss': _cmd_text,
488-
'mefd': _cmd_text,
489-
'msfd': _cmd_text,
453+
'as': _cmd_as,
454+
'ds': _cmd_ds,
455+
'is': _cmd_is,
456+
'rplc': _cmd_rplc, # rplc is called as the first edit when a doc is created from a template
457+
'rvrt': _cmd_rplc, # apparently logged after an undo
458+
'iss': _cmd_null, # suggested insertion (ignored)
459+
'mefd': _cmd_null, # suggestion
460+
'msfd': _cmd_null, # suggestion
490461
'null': _cmd_null,
491-
'ord': _cmd_text,
492-
'ras': _cmd_text,
493-
'rplc': _cmd_text,
494-
'rte': _cmd_text,
495-
'rue': _cmd_text,
496-
'rvrt': _cmd_text,
497-
'sas': _cmd_text,
498-
'sl': _cmd_text,
499-
'ste': _cmd_text,
500-
'sue': _cmd_text,
501-
'uefd': _cmd_text,
502-
'use': _cmd_text,
503-
'umv': _cmd_text,
504-
'usfd': _cmd_text,
505-
'ase': _cmd_null,
506-
'ast': _cmd_null,
507-
'astss': _cmd_null,
462+
'ord': _cmd_null,
463+
'ras': _cmd_null, # suggestion. Autospell?
464+
'rte': _cmd_null, # suggestion
465+
'rue': _cmd_null, # suggestion
466+
'sas': _cmd_null, # suggestion. Autospell?
467+
'sl': _cmd_null,
468+
'ste': _cmd_null, # suggestion
469+
'sue': _cmd_null, # suggestion
470+
'uefd': _cmd_null, # suggestion
471+
'use': _cmd_null, # suggestion
472+
'umv': _cmd_null,
473+
'usfd': _cmd_null, # suggestion
474+
'ase': _cmd_null, # suggestion
475+
'ast': _cmd_null, # suggestion. Image?
476+
'astss': _cmd_null, # suggestion. Autospell?
508477
'ue': _cmd_null,
509478
'de': _cmd_null,
510-
'dse': _cmd_null,
511-
'dss': _cmd_null,
479+
'dse': _cmd_null, # suggestion
480+
'dss': _cmd_null, # suggested deletion
512481
}
513482

514483

0 commit comments

Comments
 (0)