Skip to content

Commit f833bab

Browse files
committed
File reorganization an code cleanup
1 parent 573003d commit f833bab

File tree

7 files changed

+73
-98
lines changed

7 files changed

+73
-98
lines changed

examples/data-objects/text-editor/src/app.tsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,26 @@ async function initFluid(): Promise<DualUserViews> {
156156
}
157157
}
158158

159-
const viewLabels: Record<ViewType, string> = {
160-
plainTextarea: "Plain Textarea",
161-
plainQuill: "Plain Quill",
162-
formatted: "Formatted Quill",
163-
};
159+
const viewLabels = {
160+
plainTextarea: {
161+
description: "Plain Textarea",
162+
component: (root: TextEditorRoot) => (
163+
<PlainTextMainView root={toPropTreeNode(root.plainText)} />
164+
),
165+
},
166+
plainQuill: {
167+
description: "Plain Quill Editor",
168+
component: (root: TextEditorRoot) => (
169+
<PlainQuillView root={toPropTreeNode(root.plainText)} />
170+
),
171+
},
172+
formatted: {
173+
description: "Formatted Quill Editor",
174+
component: (root: TextEditorRoot) => (
175+
<FormattedMainView root={toPropTreeNode(root.formattedText)} />
176+
),
177+
},
178+
} as const;
164179

165180
const UserPanel: React.FC<{
166181
label: string;
@@ -169,17 +184,8 @@ const UserPanel: React.FC<{
169184
treeView: TreeView<typeof TextEditorRoot>;
170185
}> = ({ label, color, viewType, treeView }) => {
171186
const renderView = (): JSX.Element => {
172-
switch (viewType) {
173-
case "plainTextarea": {
174-
return <PlainTextMainView root={toPropTreeNode(treeView.root.plainText)} />;
175-
}
176-
case "plainQuill": {
177-
return <PlainQuillView root={toPropTreeNode(treeView.root.plainText)} />;
178-
}
179-
default: {
180-
return <FormattedMainView root={toPropTreeNode(treeView.root.formattedText)} />;
181-
}
182-
}
187+
const root = treeView.root;
188+
return viewLabels[viewType].component(root);
183189
};
184190

185191
return (
@@ -238,7 +244,7 @@ const App: React.FC<{ views: DualUserViews }> = ({ views }) => {
238244
>
239245
{(Object.keys(viewLabels) as ViewType[]).map((type) => (
240246
<option key={type} value={type}>
241-
{viewLabels[type]}
247+
{viewLabels[type].description}
242248
</option>
243249
))}
244250
</select>

examples/data-objects/text-editor/src/formatted/formattedSchema.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/data-objects/text-editor/src/formatted/formattedTextEditorFactory.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

examples/data-objects/text-editor/src/formatted/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Licensed under the MIT License.
44
*/
55

6-
export { FormattedTextAsTree, formattedTreeConfiguration } from "./formattedSchema.js";
7-
export { FormattedTextEditorFactory } from "./formattedTextEditorFactory.js";
8-
export { FormattedMainView, type FormattedMainViewProps } from "./quillFormattedView.js";
6+
export {
7+
FormattedTextAsTree,
8+
FormattedTextEditorFactory,
9+
FormattedMainView,
10+
type FormattedMainViewProps,
11+
} from "./quillFormattedView.js";

examples/data-objects/text-editor/src/formatted/quillFormattedView.tsx

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,44 @@
44
*/
55

66
import { type PropTreeNode, unwrapPropTreeNode } from "@fluidframework/react/alpha";
7-
import { Tree } from "@fluidframework/tree";
7+
// eslint-disable-next-line import-x/no-internal-modules
8+
import { treeDataObjectInternal } from "@fluidframework/react/internal";
9+
import { Tree, TreeViewConfiguration } from "@fluidframework/tree";
10+
// eslint-disable-next-line import-x/no-internal-modules
11+
import { FormattedTextAsTree } from "@fluidframework/tree/internal";
12+
// eslint-disable-next-line import-x/no-internal-modules
13+
export { FormattedTextAsTree } from "@fluidframework/tree/internal";
814
import Quill from "quill";
915
import Delta from "quill-delta";
1016
import * as React from "react";
1117

12-
import { FormattedTextAsTree } from "./formattedSchema.js";
18+
export const FormattedTextEditorFactory = treeDataObjectInternal(
19+
new TreeViewConfiguration({ schema: FormattedTextAsTree.Tree }),
20+
() => FormattedTextAsTree.Tree.fromString(""),
21+
).factory;
1322

1423
/**
1524
* Represents a single operation in a Quill Delta.
1625
* Deltas describe changes to document content as a sequence of operations.
1726
*/
1827
interface QuillDeltaOp {
19-
// Text or embed object to insert at current position
28+
/** Text or embed object to insert at current position. */
2029
insert?: string | Record<string, unknown>;
21-
// Number of characters to delete at current position
30+
/** Number of characters to delete at current position. */
2231
delete?: number;
23-
// Number of characters to keep/skip, optionally applying attributes
32+
/** Number of characters to keep/skip, optionally applying attributes. */
2433
retain?: number;
25-
// Formatting attributes (bold, italic, size, etc.) for insert/retain ops
34+
/** Formatting attributes (bold, italic, size, etc.) for insert/retain ops. */
2635
attributes?: Record<string, unknown>;
2736
}
2837

29-
// Represents a Quill Delta
38+
/** Represents a Quill Delta. */
3039
interface QuillDelta {
31-
// Sequence of operations that make up this delta
40+
/** Sequence of operations that make up this delta. */
3241
ops?: QuillDeltaOp[];
3342
}
3443

35-
// Props for the FormattedMainView component.
44+
/** Props for the FormattedMainView component. */
3645
export interface FormattedMainViewProps {
3746
root: PropTreeNode<FormattedTextAsTree.Tree>;
3847
}
@@ -41,25 +50,25 @@ export const FormattedMainView: React.FC<FormattedMainViewProps> = ({ root }) =>
4150
return <FormattedTextEditorView root={root} />;
4251
};
4352

44-
// Quill size names mapped to pixel values for tree storage
45-
const SIZE_MAP: Record<string, number> = { small: 10, large: 18, huge: 24 };
46-
// Reverse mapping: pixel values back to Quill size names for display
47-
const SIZE_REVERSE: Record<number, string> = { 10: "small", 18: "large", 24: "huge" };
48-
// Default formatting values when no explicit format is specified
49-
const DEFAULT_SIZE = 12;
50-
const DEFAULT_FONT = "Arial";
53+
/** Quill size names mapped to pixel values for tree storage. */
54+
const sizeMap = { small: 10, large: 18, huge: 24 } as const;
55+
/** Reverse mapping: pixel values back to Quill size names for display. */
56+
const sizeReverse = { 10: "small", 18: "large", 24: "huge" } as const;
57+
/** Default formatting values when no explicit format is specified. */
58+
const defaultSize = 12;
59+
/** Default font when no explicit font is specified. */
60+
const defaultFont = "Arial";
5161

5262
/**
5363
* Parse a size value from Quill into a numeric pixel value.
5464
* Handles Quill's named sizes (small, large, huge), numeric values, and pixel strings.
5565
*/
5666
function parseSize(size: unknown): number {
5767
if (typeof size === "number") return size;
58-
if (typeof size === "string") {
59-
// Try named size first, then parse as number, fallback to default
60-
return SIZE_MAP[size] ?? (Number.parseInt(size, 10) || DEFAULT_SIZE);
68+
if (size === "small" || size === "large" || size === "huge") {
69+
return sizeMap[size];
6170
}
62-
return DEFAULT_SIZE;
71+
return defaultSize;
6372
}
6473

6574
/**
@@ -79,7 +88,7 @@ function quillAttrsToFormat(attrs?: Record<string, unknown>): {
7988
italic: attrs?.italic === true,
8089
underline: attrs?.underline === true,
8190
size: parseSize(attrs?.size),
82-
font: typeof attrs?.font === "string" ? attrs.font : DEFAULT_FONT,
91+
font: typeof attrs?.font === "string" ? attrs.font : defaultFont,
8392
};
8493
}
8594

@@ -99,8 +108,7 @@ function quillAttrsToPartial(
99108
if ("italic" in attrs) format.italic = attrs.italic === true;
100109
if ("underline" in attrs) format.underline = attrs.underline === true;
101110
if ("size" in attrs) format.size = parseSize(attrs.size);
102-
if ("font" in attrs)
103-
format.font = typeof attrs.font === "string" ? attrs.font : DEFAULT_FONT;
111+
if ("font" in attrs) format.font = typeof attrs.font === "string" ? attrs.font : defaultFont;
104112
return format;
105113
}
106114

@@ -117,11 +125,11 @@ function formatToQuillAttrs(
117125
if (format.bold) attrs.bold = true;
118126
if (format.italic) attrs.italic = true;
119127
if (format.underline) attrs.underline = true;
120-
if (format.size !== DEFAULT_SIZE) {
128+
if (format.size !== defaultSize) {
121129
// Convert pixel value back to Quill size name if possible
122-
attrs.size = SIZE_REVERSE[format.size] ?? `${format.size}px`;
130+
attrs.size = sizeReverse[format.size] ?? `${format.size}px`;
123131
}
124-
if (format.font !== DEFAULT_FONT) attrs.font = format.font;
132+
if (format.font !== defaultFont) attrs.font = format.font;
125133
return attrs;
126134
}
127135

examples/data-objects/text-editor/src/index.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import { ContainerViewRuntimeFactory } from "@fluid-example/example-utils";
77
import type { IReactTreeDataObject } from "@fluidframework/react/alpha";
88
import * as React from "react";
99

10-
import {
11-
type FormattedTextAsTree,
12-
FormattedTextEditorFactory,
13-
FormattedMainView,
14-
} from "./formatted/index.js";
1510
import {
1611
type TextAsTree,
1712
TextEditorFactory,
@@ -22,13 +17,13 @@ import {
2217

2318
/**
2419
* Injected by webpack DefinePlugin.
25-
* Set via --env view=quill, --env view=plaintext, or --env view=formatted
20+
* Set via --env view=quill or --env view=plaintext
2621
*/
27-
declare const __FLUID_VIEW__: "quill" | "plaintext" | "formatted";
22+
declare const __FLUID_VIEW__: "quill" | "plaintext";
2823

2924
/**
3025
* Get the view component based on webpack build configuration.
31-
* Set via --env view=quill, --env view=plaintext, or --env view=formatted
26+
* Set via --env view=quill or --env view=plaintext
3227
*/
3328
function getViewComponent(): React.FC<MainViewProps> {
3429
if (__FLUID_VIEW__ === "plaintext") {
@@ -42,17 +37,6 @@ function getViewComponent(): React.FC<MainViewProps> {
4237
*/
4338
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4439
function createFluidExport() {
45-
if (__FLUID_VIEW__ === "formatted") {
46-
return new ContainerViewRuntimeFactory(
47-
FormattedTextEditorFactory,
48-
(tree: IReactTreeDataObject<typeof FormattedTextAsTree.Tree>) => {
49-
return React.createElement(tree.TreeViewComponent, {
50-
viewComponent: FormattedMainView,
51-
});
52-
},
53-
);
54-
}
55-
5640
return new ContainerViewRuntimeFactory(
5741
TextEditorFactory,
5842
(tree: IReactTreeDataObject<typeof TextAsTree.Tree>) => {

examples/data-objects/text-editor/src/test/textEditor.test.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import { independentView } from "@fluidframework/tree/internal";
1212
import { render } from "@testing-library/react";
1313
import * as React from "react";
1414

15-
import { FormattedTextAsTree } from "../formatted/formattedSchema.js";
16-
import { FormattedMainView } from "../formatted/quillFormattedView.js";
17-
import { MainView as PlainTextMainView } from "../plain/plainTextView.js";
18-
import { MainView as QuillMainView, type MainViewProps } from "../plain/quillView.js";
19-
import { TextAsTree } from "../plain/schema.js";
15+
import { FormattedTextAsTree, FormattedMainView } from "../formatted/quillFormattedView.js";
16+
import {
17+
PlainTextMainView,
18+
QuillMainView,
19+
TextAsTree,
20+
type MainViewProps,
21+
} from "../plain/index.js";
2022

2123
// Configuration for creating formatted text views
2224
const formattedTreeConfig = new TreeViewConfiguration({ schema: FormattedTextAsTree.Tree });

0 commit comments

Comments
 (0)