/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import { DataObject, DataObjectFactory, TreeDataObject, TreeDataObjectFactory, } from "@fluidframework/aqueduct/internal"; import { SharedCell } from "@fluidframework/cell/internal"; import type { IFluidHandle, IFluidLoadable } from "@fluidframework/core-interfaces"; import { SharedCounter } from "@fluidframework/counter/internal"; import { SharedMatrix } from "@fluidframework/matrix/internal"; import { SharedString } from "@fluidframework/sequence/internal"; import { type ITree, SchemaFactory, TreeViewConfiguration } from "@fluidframework/tree"; import { SharedTree, type TreeView } from "@fluidframework/tree/internal"; import { v4 as uuid } from "uuid"; import { TodoList, TodoItem } from "./Schema.js"; /** * Props used when creating a new todo item. */ interface TodoItemProps { /** * The initial text to populate the todo item's title with. * This value will be inserted into the shared string at index 0. */ readonly startingText: string; } /** * Object so we can test {@link TreeDataObject} inside {@link AppData}. */ export class AppDataTree extends TreeDataObject { public static readonly Name = "@devtools-example/test-app-tree-data-object"; private readonly config = new TreeViewConfiguration({ schema: TodoList }); private static readonly factory = new TreeDataObjectFactory({ type: AppDataTree.Name, ctor: AppDataTree, sharedObjects: [SharedString.getFactory(), SharedTree.getFactory()], }); public static getFactory(): TreeDataObjectFactory { return AppDataTree.factory; } #treeView: TreeView | undefined; /** * The schema-aware view of the tree. */ public get treeView(): TreeView { if (this.#treeView === undefined) { throw new Error("treeView has not been initialized."); } return this.#treeView; } protected override async initializingFirstTime(): Promise { this.#treeView = this.tree.viewWith(this.config); if (!this.treeView.compatibility.canInitialize) { throw new Error("Incompatible schema"); } const title = SharedString.create(this.runtime); title.insertText(0, "Title"); this.treeView.initialize(new TodoList({ title: title.handle, items: [] })); } protected override async initializingFromExisting(): Promise { this.#treeView = this.tree.viewWith(this.config); if (!this.treeView.compatibility.canView) { throw new Error("Incompatible schema"); } } /** * Adds a new todo item to the list. * * @privateRemarks * This method was placed in the data object (instead of the TodoList schema class), * as we needed access to the runtime to create the `SharedString`. */ public async addTodoItem(props?: TodoItemProps): Promise { const title = SharedString.create(this.runtime); const newItemText = props?.startingText ?? "New Item"; title.insertText(0, newItemText); const description = SharedString.create(this.runtime); const todoItem = new TodoItem({ title: title.handle, description: description.handle, completed: false, }); // TODO: We should consider creating a separate field for date, so that we do not need to // concatenate it to the id. // Generate an ID that we can sort on later, and store the todo item. const id = `${Date.now()}-${uuid()}`; this.treeView.root.items.set(id, todoItem); } } /** * Additional Data Object added to the {@link AppData}. */ export class AppDataTwo extends DataObject { /** * Key in the app's `rootMap` under which the SharedString object is stored. */ private readonly sharedTextKey = "shared-text"; public static readonly Name = "@devtools-example/test-app-2"; private _text: SharedString | undefined; public get text(): SharedString { if (this._text === undefined) { throw new Error("The SharedString was not initialized correctly"); } return this._text; } private static readonly factory = new DataObjectFactory({ type: AppDataTwo.Name, ctor: AppDataTwo, sharedObjects: [SharedString.getFactory()], }); public static getFactory(): DataObjectFactory { return this.factory; } protected async initializingFirstTime(): Promise { // Create the shared objects and store their handles in the root SharedDirectory const text = SharedString.create(this.runtime, this.sharedTextKey); this.root.set(this.sharedTextKey, text.handle); this.root.set("test-object-two", { a: true, b: "hello world", c: 1, }); } protected async hasInitialized(): Promise { this._text = await this.root.get>(this.sharedTextKey)?.get(); } } /** * AppData uses the React CollaborativeTextArea to load a collaborative HTML