Skip to content

Commit e744f9d

Browse files
committed
whiteboard: much less awkward use of useFrameContext hook
1 parent 442039f commit e744f9d

File tree

16 files changed

+63
-53
lines changed

16 files changed

+63
-53
lines changed

src/packages/frontend/frame-editors/frame-tree/frame-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { createContext, useContext } from "react";
1515
import { Actions } from "../code-editor/actions";
1616
import { Map } from "immutable";
1717

18-
interface IFrameContext {
18+
export interface IFrameContext {
1919
id: string;
2020
project_id: string;
2121
path: string;

src/packages/frontend/frame-editors/whiteboard-editor/canvas.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ import RenderElement from "./elements/render";
2020
import Focused, { FOCUSED_BORDER_COLOR } from "./focused";
2121
import NotFocused from "./not-focused";
2222
import Position from "./position";
23-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
23+
import { useFrameContext } from "./hooks";
2424
import usePinchToZoom from "@cocalc/frontend/frame-editors/frame-tree/pinch-to-zoom";
2525
import Grid from "./elements/grid";
26-
27-
import { Actions } from "./actions";
2826
import {
2927
fontSizeToZoom,
3028
getPageSpan,
@@ -121,7 +119,6 @@ export default function Canvas({
121119
}, [font_size, scale, transforms.width, transforms.height]);
122120

123121
const frame = useFrameContext();
124-
const actions = frame.actions as Actions;
125122

126123
// handle setting a center position for the visible window
127124
useEffect(() => {
@@ -167,7 +164,7 @@ export default function Canvas({
167164

168165
useEffect(() => {
169166
if (fitToScreen) {
170-
actions.set_frame_tree({ id: frame.id, fitToScreen: false });
167+
frame.actions.set_frame_tree({ id: frame.id, fitToScreen: false });
171168
}
172169
}, [fitToScreen]);
173170

@@ -315,7 +312,7 @@ export default function Canvas({
315312
y: (visible.yMax + visible.yMin) / 2,
316313
};
317314
const { x, y } = data;
318-
actions.setVisibleWindowCenter(frame.id, {
315+
frame.actions.setVisibleWindowCenter(frame.id, {
319316
x: ctr.x + (x - x0) / canvasScale,
320317
y: ctr.y + (y - y0) / canvasScale,
321318
});
@@ -365,7 +362,7 @@ export default function Canvas({
365362
if (e.target == gridDivRef.current) {
366363
// clear selection
367364
// unfocus, because nothing got clicked on.
368-
actions.setFocusedElement(frame.id, "");
365+
frame.actions.setFocusedElement(frame.id, "");
369366
} else {
370367
// clicked on an element on the canvas; either stay selected or let
371368
// it handle selecting it.
@@ -393,9 +390,9 @@ export default function Canvas({
393390
z: transforms.zMax + 1,
394391
};
395392

396-
const { id } = actions.createElement(element, true);
397-
actions.setSelectedTool(frame.id, "select");
398-
actions.setFocusedElement(frame.id, id);
393+
const { id } = frame.actions.createElement(element, true);
394+
frame.actions.setSelectedTool(frame.id, "select");
395+
frame.actions.setFocusedElement(frame.id, id);
399396
}
400397
}
401398

@@ -415,7 +412,7 @@ export default function Canvas({
415412
);
416413
const xMax = xMin + width / canvasScale;
417414
const yMax = yMin + height / canvasScale;
418-
actions.saveVisibleWindow(frame.id, { xMin, yMin, xMax, yMax });
415+
frame.actions.saveVisibleWindow(frame.id, { xMin, yMin, xMax, yMax });
419416
}, 50);
420417
}, [transforms, canvasScale]);
421418

@@ -468,7 +465,7 @@ export default function Canvas({
468465
pt.y = pt.y - yMin;
469466
}
470467

471-
actions.createElement(
468+
frame.actions.createElement(
472469
{
473470
x: xMin,
474471
y: yMin,
@@ -533,7 +530,7 @@ export default function Canvas({
533530
navDrag.current = null;
534531
return;
535532
}
536-
actions.setVisibleWindowCenter(frame.id, evtToData(e));
533+
frame.actions.setVisibleWindowCenter(frame.id, evtToData(e));
537534
return;
538535
}
539536
if (!readOnly) {

src/packages/frontend/frame-editors/whiteboard-editor/elements/code.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useEffect, useState } from "react";
22
import { Input } from "antd";
3-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
4-
import { Actions } from "../actions";
3+
import { useFrameContext } from "../hooks";
54
import { Element } from "../types";
65
import { Markdown } from "@cocalc/frontend/components";
76

@@ -55,8 +54,7 @@ export default function Code({ element, focused }: Props) {
5554
setValue(e.target.value);
5655
}}
5756
onBlur={() => {
58-
const actions = frame.actions as Actions;
59-
actions.setElement({ id: element.id, str: value });
57+
frame.actions.setElement({ id: element.id, str: value });
6058
}}
6159
/>
6260
);

src/packages/frontend/frame-editors/whiteboard-editor/elements/text.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { useEffect, useState } from "react";
22
import { Markdown } from "@cocalc/frontend/components";
33
import { Input, Popover } from "antd";
4-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
5-
import { Actions } from "../actions";
4+
import { useFrameContext } from "../hooks";
65
import { Icon } from "@cocalc/frontend/components/icon";
76
import { Element } from "../types";
87
import { path_split } from "@cocalc/util/misc";
@@ -52,8 +51,7 @@ export default function Text({ element, focused }: Props) {
5251
setValue(e.target.value);
5352
}}
5453
onBlur={() => {
55-
const actions = frame.actions as Actions;
56-
actions.setElement({ id: element.id, str: value });
54+
frame.actions.setElement({ id: element.id, str: value });
5755
}}
5856
/>
5957
</div>

src/packages/frontend/frame-editors/whiteboard-editor/focused-resize.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { Tooltip } from "antd";
77
import { Icon } from "@cocalc/frontend/components/icon";
88
import { Element } from "./types";
99
import { CSSProperties, useState } from "react";
10-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
11-
import { Actions } from "./actions";
10+
import { useFrameContext } from "./hooks";
1211
import Draggable from "react-draggable";
1312
import { getPosition } from "./math";
1413

@@ -97,7 +96,6 @@ export default function DragHandle({
9796
setOffset(getOffset(data));
9897
}}
9998
onStop={(_, data) => {
100-
const actions = frame.actions as Actions;
10199
let { w, h, x, y } = getPosition(element);
102100
const offset = getOffset(data);
103101

@@ -119,7 +117,7 @@ export default function DragHandle({
119117
path[i] = element.data.path[i] * scale_x;
120118
path[i + 1] = element.data.path[i + 1] * scale_y;
121119
}
122-
actions.setElement({
120+
frame.actions.setElement({
123121
id: element.id,
124122
x,
125123
y,
@@ -128,7 +126,7 @@ export default function DragHandle({
128126
data: { ...element.data, path },
129127
});
130128
} else {
131-
actions.setElement({ id: element.id, x, y, w, h });
129+
frame.actions.setElement({ id: element.id, x, y, w, h });
132130
}
133131
}, 0);
134132
}}

src/packages/frontend/frame-editors/whiteboard-editor/focused.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import { ReactNode, useMemo, useRef, useState } from "react";
1212
import Draggable from "react-draggable";
1313
import { getAngle, getPosition } from "./math";
1414
import { Icon } from "@cocalc/frontend/components/icon";
15-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
16-
import { Actions } from "./actions";
15+
import { useFrameContext } from "./hooks";
1716
import EditBar from "./tools/edit-bar";
1817
import { Element } from "./types";
1918
import DragHandle from "./focused-resize";
@@ -120,9 +119,8 @@ export default function Focused({
120119
break;
121120
}
122121
}
123-
const actions = frame.actions as Actions;
124122
setTimeout(() => {
125-
actions.setElement({ id, rotate });
123+
frame.actions.setElement({ id, rotate });
126124
setRotating(undefined);
127125
}, 0);
128126
}}
@@ -215,8 +213,7 @@ export default function Focused({
215213
const { id } = element;
216214
const x = element.x + data.x;
217215
const y = element.y + data.y;
218-
const actions = frame.actions as Actions;
219-
actions.setElement({ id, x, y });
216+
frame.actions.setElement({ id, x, y });
220217
}}
221218
>
222219
<div
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
useFrameContext as useFrameContextGeneric,
3+
IFrameContext,
4+
} from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
5+
import { Actions } from "./actions";
6+
7+
// https://stackoverflow.com/questions/41285211/overriding-interface-property-type-defined-in-typescript-d-ts-file
8+
type Modify<T, R> = Omit<T, keyof R> & R;
9+
10+
type WhiteboardFrameContext = Modify<
11+
IFrameContext,
12+
{
13+
actions: Actions;
14+
}
15+
>;
16+
17+
export function useFrameContext(): WhiteboardFrameContext {
18+
return useFrameContextGeneric() as WhiteboardFrameContext;
19+
}

src/packages/frontend/frame-editors/whiteboard-editor/not-focused.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ReactNode } from "react";
2-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
3-
import { Actions } from "./actions";
2+
import { useFrameContext } from "./hooks";
43

54
interface Props {
65
children: ReactNode;
@@ -23,7 +22,8 @@ export default function NotFocused({
2322
!readOnly && selectable
2423
? (e) => {
2524
e.stopPropagation();
26-
(frame.actions as Actions).setFocusedElement(frame.id, id);
25+
console.log(e);
26+
frame.actions.setFocusedElement(frame.id, id);
2727
}
2828
: undefined
2929
}

src/packages/frontend/frame-editors/whiteboard-editor/time-travel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Viewer used by time travel to show whiteboard canvas at a particular point in ti
33
*/
44
import Canvas from "./canvas";
55
import NavigationPanel from "./tools/navigation";
6-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
6+
import { useFrameContext } from "./hooks";
77

88
export default function WhiteboardTimeTravel({ syncdb, version, font_size }) {
99
const { isFocused } = useFrameContext();

src/packages/frontend/frame-editors/whiteboard-editor/tools/edit-bar.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { Option } = Select;
88
import { Element } from "../types";
99
import { PANEL_STYLE } from "./panel";
1010
import { Icon } from "@cocalc/frontend/components/icon";
11-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
11+
import { useFrameContext } from "../hooks";
1212
import { Actions } from "../actions";
1313
import { BrushPreview, maxRadius } from "./pen";
1414
import ColorPicker from "@cocalc/frontend/components/color-picker";
@@ -27,8 +27,7 @@ interface Props {
2727
}
2828

2929
export default function EditBar({ elements }: Props) {
30-
const frame = useFrameContext();
31-
const actions = frame.actions as Actions;
30+
const { actions } = useFrameContext();
3231
if (elements.length == 0) return null;
3332
return (
3433
<div

src/packages/frontend/frame-editors/whiteboard-editor/tools/navigation.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ high level map view.
99
import { ReactNode, useState } from "react";
1010
import { Icon, IconName } from "@cocalc/frontend/components/icon";
1111
import { Button, Tooltip } from "antd";
12-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
13-
import { getPageSpan, fontSizeToZoom, ZOOM100 } from "../math";
12+
import { useFrameContext } from "../hooks";
1413
import { Actions } from "../actions";
14+
import { getPageSpan, fontSizeToZoom, ZOOM100 } from "../math";
1515
import { PANEL_STYLE } from "./panel";
1616
import Canvas from "../canvas";
1717
import { Element } from "../types";

src/packages/frontend/frame-editors/whiteboard-editor/tools/note.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Button, Popconfirm, Slider, Tooltip } from "antd";
77
import { PANEL_STYLE } from "./panel";
88
import { Icon } from "@cocalc/frontend/components/icon";
99
import ColorPicker from "@cocalc/frontend/components/color-picker";
10-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
10+
import { useFrameContext } from "../hooks";
1111
import { debounce } from "lodash";
1212
import { STYLE } from "../elements/note";
1313
import { DEFAULT_FONT_SIZE, minFontSize, maxFontSize } from "./defaults";

src/packages/frontend/frame-editors/whiteboard-editor/tools/panel.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import { CSSProperties, ReactNode } from "react";
88
import { Button, Tooltip } from "antd";
99
import { Icon } from "@cocalc/frontend/components/icon";
1010
import { TOOLS } from "./spec";
11-
//import Draggable from "react-draggable";
12-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
13-
import { Actions } from "../actions";
11+
import { useFrameContext } from "../hooks";
1412

1513
export const PANEL_STYLE = {
1614
zIndex: 1000,
@@ -30,7 +28,14 @@ export default function Panel({ selectedTool }) {
3028
);
3129
}
3230
return (
33-
<div style={{ ...PANEL_STYLE, width:'46px', display: "flex", flexDirection: "column" }}>
31+
<div
32+
style={{
33+
...PANEL_STYLE,
34+
width: "46px",
35+
display: "flex",
36+
flexDirection: "column",
37+
}}
38+
>
3439
{v}
3540
</div>
3641
);
@@ -44,7 +49,7 @@ function ToolButton({ tool, isSelected }) {
4449
<Button
4550
type="text"
4651
onClick={() => {
47-
(actions as Actions).setSelectedTool(id, tool);
52+
actions.setSelectedTool(id, tool);
4853
}}
4954
>
5055
<Icon

src/packages/frontend/frame-editors/whiteboard-editor/tools/pen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Button, Slider, Tooltip } from "antd";
77
import { PANEL_STYLE } from "./panel";
88
import { Icon } from "@cocalc/frontend/components/icon";
99
import ColorPicker from "@cocalc/frontend/components/color-picker";
10-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
10+
import { useFrameContext } from "../hooks";
1111
import { debounce } from "lodash";
1212
import { ResetButton } from "./note";
1313

src/packages/frontend/frame-editors/whiteboard-editor/tools/upload.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { ReactNode, RefObject, useRef } from "react";
22
import { Dropzone, FileUploadWrapper } from "@cocalc/frontend/file-upload";
33
import { aux_file } from "@cocalc/util/misc";
44
const AUX_FILE_EXT = "upload";
5-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
6-
import { Actions } from "../actions";
5+
import { useFrameContext } from "../hooks";
76
import { join } from "path";
87

98
interface Props {
@@ -35,7 +34,7 @@ export default function Upload({ children, evtToDataRef }: Props) {
3534
// is an image
3635
str = `<img src="${filename}" style="object-fit:cover"/>`;
3736
}
38-
(actions as Actions).createElement({
37+
actions.createElement({
3938
...location,
4039
type: "text",
4140
str,

src/packages/frontend/frame-editors/whiteboard-editor/whiteboard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ToolPanel from "./tools/panel";
88
import PenPanel from "./tools/pen";
99
import NotePanel from "./tools/note";
1010
import NavigationPanel from "./tools/navigation";
11-
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
11+
import { useFrameContext } from "./hooks";
1212
import Upload from "./tools/upload";
1313

1414
interface Props {

0 commit comments

Comments
 (0)