diff options
Diffstat (limited to 'src/new_fields')
| -rw-r--r-- | src/new_fields/Doc.ts | 30 | ||||
| -rw-r--r-- | src/new_fields/Types.ts | 8 |
2 files changed, 37 insertions, 1 deletions
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts index bd10e5474..79e5a156d 100644 --- a/src/new_fields/Doc.ts +++ b/src/new_fields/Doc.ts @@ -5,6 +5,9 @@ import { Utils } from "../Utils"; import { DocServer } from "../client/DocServer"; import { setter, getter, getField } from "./util"; import { Cast, ToConstructor, PromiseValue, FieldValue } from "./Types"; +import { UndoManager, undoBatch } from "../client/util/UndoManager"; +import { listSpec } from "./Schema"; +import { List } from "./List"; export type FieldId = string; export const HandleUpdate = Symbol("HandleUpdate"); @@ -54,7 +57,7 @@ export class Doc extends RefField { return doc; } - proto: FieldResult<Doc>; + proto: Opt<Doc>; [key: string]: FieldResult; @serializable(alias("fields", map(autoObject()))) @@ -126,6 +129,31 @@ export namespace Doc { return alias; } + export function MakeLink(source: Doc, target: Doc): Doc { + let linkDoc = new Doc; + UndoManager.RunInBatch(() => { + linkDoc.title = "New Link"; + linkDoc.linkDescription = ""; + linkDoc.linkTags = "Default"; + + linkDoc.linkedTo = target; + linkDoc.linkedFrom = source; + + let linkedFrom = Cast(target.linkedFromDocs, listSpec(Doc)); + if (!linkedFrom) { + target.linkedFromDocs = linkedFrom = new List<Doc>(); + } + linkedFrom.push(linkDoc); + + let linkedTo = Cast(source.linkedToDocs, listSpec(Doc)); + if (!linkedTo) { + source.linkedToDocs = linkedTo = new List<Doc>(); + } + linkedTo.push(linkDoc); + }, "make link"); + return linkDoc; + } + export function MakeDelegate(doc: Doc): Doc; export function MakeDelegate(doc: Opt<Doc>): Opt<Doc>; export function MakeDelegate(doc: Opt<Doc>): Opt<Doc> { diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts index 4ad8dcade..079c7b76d 100644 --- a/src/new_fields/Types.ts +++ b/src/new_fields/Types.ts @@ -59,6 +59,14 @@ export function Cast<T extends ToConstructor<Field> | ListSpec<Field>>(field: Fi return defaultVal; } +export function NumCast(field: FieldResult, defaultVal: Opt<number> = 0) { + return Cast(field, "number", defaultVal); +} + +export function StrCast(field: FieldResult, defaultVal: Opt<string> = "") { + return Cast(field, "string", defaultVal); +} + type WithoutList<T extends Field> = T extends List<infer R> ? R[] : T; export function FieldValue<T extends Field, U extends WithoutList<T>>(field: Opt<T> | Promise<Opt<T>>, defaultValue: U): WithoutList<T>; |
