diff options
author | Sam Wilkins <samwilkins333@gmail.com> | 2020-02-08 17:32:33 -0500 |
---|---|---|
committer | Sam Wilkins <samwilkins333@gmail.com> | 2020-02-08 17:32:33 -0500 |
commit | 79e1323acd0d0f95d08a09cefce908e35d0e7558 (patch) | |
tree | 3ee8c780fca7d8a7a35847af5f59d2d3fd0875f6 /src/client/documents/Documents.ts | |
parent | 36933b7b647a54aa7bda0600612d34b402d42919 (diff) |
initial commit, pending png read error bug fix
Diffstat (limited to 'src/client/documents/Documents.ts')
-rw-r--r-- | src/client/documents/Documents.ts | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index d647b34e6..f05bb3736 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -54,6 +54,7 @@ import { InkField } from "../../new_fields/InkField"; import { InkingControl } from "../views/InkingControl"; import { RichTextField } from "../../new_fields/RichTextField"; import { Networking } from "../Network"; +import { extname } from "path"; const requestImageSize = require('../util/request-image-size'); const path = require('path'); @@ -341,8 +342,34 @@ export namespace Docs { */ export namespace Create { - export async function Buxton() { - console.log(await Networking.FetchFromServer("/newBuxton")); + export function Buxton() { + const loading = new Doc; + loading.title = "Please wait for the import script..."; + const parent = TreeDocument([loading], { + title: "The Buxton Collection", + _width: 400, + _height: 400 + }); + Networking.FetchFromServer("/buxton").then(response => { + parent.data = new List<Doc>(); + const devices = JSON.parse(response); + if (!Array.isArray(devices)) { + alert("Improper Buxton import formatting!"); + return; + } + devices.forEach(device => { + const { __images } = device; + delete device.__images; + const { ImageDocument, StackingDocument } = Docs.Create; + if (Array.isArray(__images)) { + const deviceImages = __images.map((url, i) => ImageDocument(url, { title: `image${i}.${extname(url)}` })); + const doc = StackingDocument(deviceImages, { title: device.title }); + Docs.Get.DocumentHierarchyFromJson(device, undefined, doc); + Doc.AddDocToList(parent, "data", doc); + } + }); + }); + return parent; } Scripting.addGlobal(Buxton); @@ -628,7 +655,7 @@ export namespace Docs { * or the result of any JSON.parse() call. * @param title an optional title to give to the highest parent document in the hierarchy */ - export function DocumentHierarchyFromJson(input: any, title?: string): Opt<Doc> { + export function DocumentHierarchyFromJson(input: any, title?: string, appendToTarget?: Doc): Opt<Doc> { if (input === undefined || input === null || ![...primitives, "object"].includes(typeof input)) { return undefined; } @@ -638,7 +665,7 @@ export namespace Docs { } let converted: Doc; if (typeof parsed === "object" && !(parsed instanceof Array)) { - converted = convertObject(parsed, title); + converted = convertObject(parsed, title, appendToTarget); } else { (converted = new Doc).json = toField(parsed); } @@ -653,12 +680,12 @@ export namespace Docs { * @returns the object mapped from JSON to field values, where each mapping * might involve arbitrary recursion (since toField might itself call convertObject) */ - const convertObject = (object: any, title?: string): Doc => { - const target = new Doc(); + const convertObject = (object: any, title?: string, target?: Doc): Doc => { + const resolved = target ?? new Doc; let result: Opt<Field>; - Object.keys(object).map(key => (result = toField(object[key], key)) && (target[key] = result)); - title && !target.title && (target.title = title); - return target; + Object.keys(object).map(key => (result = toField(object[key], key)) && (resolved[key] = result)); + title && !resolved.title && (resolved.title = title); + return resolved; }; /** |