-
Notifications
You must be signed in to change notification settings - Fork 556
Canvas view/model separation #6897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ChumpChief
merged 10 commits into
microsoft:main
from
ChumpChief:CanvasViewModelSeparation
Aug 4, 2021
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
acec6fd
Split view to separate class
ChumpChief 3c6549b
Use ContainerViewRuntimeFactory
ChumpChief 48c79e5
Drop IFluidHTMLView from DO, start conversion to react
ChumpChief cff8b7b
Add React dependency
ChumpChief 95ad074
Some react work
ChumpChief 836bb80
Start using react view
ChumpChief 6fa46d0
Make strict, remove old view, partial resize handling
ChumpChief 7c12b1c
Fix lint
ChumpChief f984994
Fix test for strict mode
ChumpChief f051c2e
Convert index to .ts file
ChumpChief File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { IColor, InkCanvas } from "@fluidframework/ink"; | ||
|
||
import React, { useEffect, useRef, useState } from "react"; | ||
|
||
import { Canvas } from "./canvas"; | ||
// eslint-disable-next-line import/no-unassigned-import | ||
import "./style.less"; | ||
|
||
const colorPickerColors: IColor[] = [ | ||
{ r: 253, g: 0, b: 12, a: 1 }, | ||
{ r: 134, g: 0, b: 56, a: 1 }, | ||
{ r: 253, g: 187, b: 48, a: 1 }, | ||
{ r: 255, g: 255, b: 81, a: 1 }, | ||
{ r: 0, g: 45, b: 98, a: 1 }, | ||
{ r: 255, g: 255, b: 255, a: 1 }, | ||
{ r: 246, g: 83, b: 20, a: 1 }, | ||
{ r: 0, g: 161, b: 241, a: 1 }, | ||
{ r: 124, g: 187, b: 0, a: 1 }, | ||
{ r: 8, g: 170, b: 51, a: 1 }, | ||
{ r: 0, g: 0, b: 0, a: 1 }, | ||
]; | ||
|
||
interface IToolbarProps { | ||
toggleColorPicker: () => void; | ||
replayInk: () => void; | ||
clearInk: () => void; | ||
} | ||
|
||
const Toolbar: React.FC<IToolbarProps> = (props: IToolbarProps) => { | ||
const { toggleColorPicker, replayInk, clearInk } = props; | ||
return ( | ||
<div className="ink-toolbar"> | ||
<button | ||
className="ink-toolbar-button fluid-icon-pencil" | ||
title="Change Color" | ||
onClick={toggleColorPicker} | ||
></button> | ||
<button | ||
className="ink-toolbar-button fluid-icon-replay" | ||
title="Replay" | ||
onClick={replayInk} | ||
></button> | ||
<button | ||
className="ink-toolbar-button fluid-icon-cross" | ||
title="Clear" | ||
onClick={clearInk} | ||
></button> | ||
</div> | ||
); | ||
}; | ||
|
||
interface IColorOptionProps { | ||
color: IColor; | ||
choose: () => void; | ||
} | ||
|
||
const ColorOption: React.FC<IColorOptionProps> = (props: IColorOptionProps) => { | ||
const { color, choose } = props; | ||
return ( | ||
<button | ||
className="ink-color-option" | ||
onClick={ choose } | ||
style={{ backgroundColor: `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})` }} | ||
></button> | ||
); | ||
}; | ||
|
||
interface IColorPickerProps { | ||
show: boolean; | ||
choose: (color: IColor) => void; | ||
} | ||
|
||
const ColorPicker: React.FC<IColorPickerProps> = (props: IColorPickerProps) => { | ||
const { show, choose } = props; | ||
return ( | ||
<div className={`ink-color-picker${show ? " show" : ""}`}> | ||
{ | ||
colorPickerColors.map((color, index) => { | ||
const pickColor = () => { | ||
choose(color); | ||
}; | ||
return <ColorOption key={index} color={color} choose={pickColor} />; | ||
}) | ||
} | ||
</div> | ||
); | ||
}; | ||
|
||
interface ICanvasViewProps { | ||
canvas: Canvas; | ||
} | ||
|
||
export const CanvasView: React.FC<ICanvasViewProps> = (props: ICanvasViewProps) => { | ||
const { canvas } = props; | ||
const [inkCanvas, setInkCanvas] = useState<InkCanvas | undefined>(undefined); | ||
const [showColorPicker, setShowColorPicker] = useState<boolean>(false); | ||
// eslint-disable-next-line no-null/no-null | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
|
||
useEffect(() => { | ||
// eslint-disable-next-line no-null/no-null | ||
if (canvasRef.current !== null && inkCanvas === undefined) { | ||
setInkCanvas(new InkCanvas(canvasRef.current, canvas.ink)); | ||
} | ||
}, [canvas, canvasRef.current]); | ||
|
||
useEffect(() => { | ||
if (inkCanvas !== undefined) { | ||
const resizeHandler = () => { | ||
inkCanvas.sizeCanvasBackingStore(); | ||
}; | ||
window.addEventListener("resize", resizeHandler); | ||
inkCanvas.sizeCanvasBackingStore(); | ||
return () => { | ||
window.removeEventListener("resize", resizeHandler); | ||
}; | ||
} | ||
}, [inkCanvas]); | ||
|
||
const toggleColorPicker = () => { | ||
setShowColorPicker(!showColorPicker); | ||
}; | ||
const replayInk = inkCanvas?.replay.bind(inkCanvas) ?? (() => {}); | ||
const clearInk = inkCanvas?.clear.bind(inkCanvas) ?? (() => {}); | ||
const chooseColor = (color: IColor) => { | ||
inkCanvas?.setPenColor(color); | ||
setShowColorPicker(false); | ||
}; | ||
return ( | ||
<div className="ink-component-root"> | ||
<div className="ink-surface"> | ||
<canvas className="ink-canvas" ref={ canvasRef }></canvas> | ||
<Toolbar | ||
toggleColorPicker={ toggleColorPicker } | ||
replayInk={ replayInk } | ||
clearInk={ clearInk } | ||
/> | ||
</div> | ||
<ColorPicker show={ showColorPicker } choose={ chooseColor } /> | ||
</div> | ||
); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant typing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally yes, but unfortunately eslint doesn't detect this correctly and errors:
jsx-eslint/eslint-plugin-react#2353