From df444357c153abf8daf87d9f80f4fe2bcfba00b9 Mon Sep 17 00:00:00 2001 From: Sam Wilkins Date: Wed, 31 Jul 2019 13:40:47 -0400 Subject: filtering --- src/client/util/DictationManager.ts | 51 ++++++++++++++++++++++++++++--------- src/new_fields/Types.ts | 8 +++--- 2 files changed, 44 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/client/util/DictationManager.ts b/src/client/util/DictationManager.ts index f0fd7cbde..9c539e86a 100644 --- a/src/client/util/DictationManager.ts +++ b/src/client/util/DictationManager.ts @@ -4,11 +4,14 @@ import { SelectionManager } from "./SelectionManager"; import { DocumentView } from "../views/nodes/DocumentView"; import { UndoManager } from "./UndoManager"; import * as converter from "words-to-numbers"; -import { Doc } from "../../new_fields/Doc"; +import { Doc, Field } from "../../new_fields/Doc"; import { List } from "../../new_fields/List"; import { Docs } from "../documents/Documents"; import { CollectionViewType } from "../views/collections/CollectionBaseView"; import { MainView } from "../views/MainView"; +import { listSpec } from "../../new_fields/Schema"; +import { Cast, ToConstructor, ListSpec, CastCtor } from "../../new_fields/Types"; +import { ImageField } from "../../new_fields/URLField"; namespace CORE { export interface IWindow extends Window { @@ -20,6 +23,7 @@ const { webkitSpeechRecognition }: CORE.IWindow = window as CORE.IWindow; export type IndependentAction = (target: DocumentView) => any | Promise; export type DependentAction = (target: DocumentView, matches: RegExpExecArray) => any | Promise; export type RegexEntry = { key: RegExp, value: DependentAction }; +export type RegistrationUnit = { filter: Predicate, action: T }; export default class DictationManager { public static Instance = new DictationManager(); @@ -58,13 +62,17 @@ export default class DictationManager { return title.replace("...", "").toLowerCase().trim(); } - public registerStatic = (keys: Array, action: IndependentAction, overwrite = false) => { + public registerStatic = (keys: Array, action: IndependentAction, filter: Predicate, overwrite = false) => { let success = true; keys.forEach(key => { key = this.sanitize(key); let existing = RegisteredCommands.Independent.get(key); if (!existing || overwrite) { - RegisteredCommands.Independent.set(key, action); + let unit = { + filter: filter, + action: action + }; + RegisteredCommands.Independent.set(key, unit); } else { success = false; } @@ -99,9 +107,9 @@ export default class DictationManager { let batch = UndoManager.StartBatch("Dictation Action"); phrase = this.sanitize(phrase); - let independentAction = RegisteredCommands.Independent.get(phrase); - if (independentAction) { - await independentAction(target); + let unit = RegisteredCommands.Independent.get(phrase); + if (unit && unit.filter(target)) { + await unit.action(target); batch.end(); return true; } @@ -127,15 +135,21 @@ export default class DictationManager { export namespace RegisteredCommands { - export const Independent = new Map([ + export const Independent = new Map>([ - ["clear", (target: DocumentView) => { - Doc.GetProto(target.props.Document).data = new List(); + ["clear", { + action: (target: DocumentView) => { + Doc.GetProto(target.props.Document).data = new List(); + }, + filter: Filters.isCollectionView }], - ["open fields", (target: DocumentView) => { - let kvp = Docs.Create.KVPDocument(target.props.Document, { width: 300, height: 300 }); - target.props.addDocTab(kvp, target.dataDoc, "onRight"); + ["open fields", { + action: (target: DocumentView) => { + let kvp = Docs.Create.KVPDocument(target.props.Document, { width: 300, height: 300 }); + target.props.addDocTab(kvp, target.dataDoc, "onRight"); + }, + filter: Filters.isImageView }] ]); @@ -177,4 +191,17 @@ export namespace RegisteredCommands { ); +} + +export type Predicate = (target: DocumentView) => boolean; + +export namespace Filters { + + const tryCast = (view: DocumentView, ctor: T) => Cast(Doc.GetProto(view.props.Document).data, ctor); + + export const isCollectionView: Predicate = (target: DocumentView) => tryCast(target, listSpec(Doc)) !== undefined; + + export const isImageView: Predicate = (target: DocumentView) => tryCast(target, ImageField) !== undefined; + + } \ No newline at end of file diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts index 565ae2ee3..09cbff25e 100644 --- a/src/new_fields/Types.ts +++ b/src/new_fields/Types.ts @@ -48,9 +48,11 @@ export interface Interface { } export type WithoutRefField = T extends RefField ? never : T; -export function Cast | ListSpec>(field: FieldResult, ctor: T): FieldResult>; -export function Cast | ListSpec>(field: FieldResult, ctor: T, defaultVal: WithoutList>> | null): WithoutList>; -export function Cast | ListSpec>(field: FieldResult, ctor: T, defaultVal?: ToType | null): FieldResult> | undefined { +export type CastCtor = ToConstructor | ListSpec; + +export function Cast(field: FieldResult, ctor: T): FieldResult>; +export function Cast(field: FieldResult, ctor: T, defaultVal: WithoutList>> | null): WithoutList>; +export function Cast(field: FieldResult, ctor: T, defaultVal?: ToType | null): FieldResult> | undefined { if (field instanceof Promise) { return defaultVal === undefined ? field.then(f => Cast(f, ctor) as any) as any : defaultVal === null ? undefined : defaultVal; } -- cgit v1.2.3-70-g09d2