Skip to content

Commit

Permalink
Explicitly provide Redux store context to Layers
Browse files Browse the repository at this point in the history
This is a known issue of react-konva, see eg.
konvajs/react-konva#349
konvajs/react-konva#311

Revert some changes made to drawControl and boardControl
reducers.
  • Loading branch information
heat1q committed Jan 28, 2021
1 parent c6a8c89 commit 446e1c0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 56 deletions.
13 changes: 4 additions & 9 deletions src/component/board/livelayer.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import React from "react"
import React, { memo } from "react"
import { FastLayer } from "react-konva"
import { useSelector } from "react-redux"
import store from "../../redux/store"
import { StrokeShape } from "./stroke"

export default function LiveLayer() {
export default memo(() => {
const pts = useSelector((state) => state.drawControl.liveStroke.points)

return (
<FastLayer>
{pts.length > 0 ? (
<StrokeShape
stroke={{
...store.getState().drawControl.liveStroke,
points: pts,
}}
/>
<StrokeShape stroke={store.getState().drawControl.liveStroke} />
) : (
<></>
)}
</FastLayer>
)
}
})
14 changes: 7 additions & 7 deletions src/component/board/page.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from "react"
import React, { memo } from "react"
import { useSelector } from "react-redux"
import { Rect } from "react-konva"
import { StrokeShape } from "./stroke"
import { CANVAS_WIDTH, CANVAS_HEIGHT } from "../../constants"
import store from "../../redux/store"

export default function Page({ pageId, isDraggable }) {
export default memo(({ pageId, isDraggable }) => {
const strokes = useSelector(
(state) => state.boardControl.present.pageCollection[pageId]
(state) => state.boardControl.present.pageCollection[pageId].strokes
)

return (
Expand All @@ -26,13 +26,13 @@ export default function Page({ pageId, isDraggable }) {
strokeWidth={5}
fill="#eee"
/>
{strokes.map((stroke) => (
{Object.keys(strokes).map((id) => (
<StrokeShape
key={stroke.id}
stroke={stroke}
key={id}
stroke={strokes[id]}
isDraggable={isDraggable}
/>
))}
</>
)
}
})
60 changes: 35 additions & 25 deletions src/component/board/stage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import { useSelector } from "react-redux"
import { ReactReduxContext, useSelector } from "react-redux"
import { Stage, Layer } from "react-konva"

import Page from "./page"
Expand All @@ -11,8 +11,8 @@ import { toolType, MIN_SAMPLE_COUNT } from "../../constants"

export default function BoardStage() {
const pageRank = useSelector((state) => state.boardControl.present.pageRank)
const isDraggable = useSelector((state) => state.drawControl.isDraggable)

const isDraggable = useSelector((state) => state.drawControl.isDraggable)
const isMouseDown = useSelector((state) => state.drawControl.isMouseDown)
const isActive = useSelector((state) => state.drawControl.isActive)
const tool = useSelector((state) => state.drawControl.liveStroke.type)
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function BoardStage() {
sampleCount = 1

const pos = getScaledPointerPosition(e)
startLiveStroke(pos)
startLiveStroke(pos, pageRank[0]) // todo get pageid
}

function onMouseMove(e) {
Expand Down Expand Up @@ -124,28 +124,38 @@ export default function BoardStage() {
return (
<div className="pagecollectionouter">
<div className="pagecollectioninner">
<Stage
draggable={!isActive}
className="stage"
width={window.innerWidth}
height={window.innerHeight}
onMouseDown={onMouseDown}
onMousemove={onMouseMove}
onMouseUp={onMouseUp}
onMouseLeave={onMouseUp}
onContextMenu={(e) => e.evt.preventDefault()}
onTouchStart={onMouseDown}
onTouchMove={onMouseMove}
onTouchEnd={onMouseUp}
onDragEnd={onDragEnd}
onWheel={onWheel}>
<Layer>
{pageRank.map((pageId) => (
<Page pageId={pageId} isDraggable={isDraggable} />
))}
</Layer>
<LiveLayer />
</Stage>
<ReactReduxContext.Consumer>
{(value) => (
<Stage
draggable={!isActive}
className="stage"
width={window.innerWidth}
height={window.innerHeight}
onMouseDown={onMouseDown}
onMousemove={onMouseMove}
onMouseUp={onMouseUp}
onMouseLeave={onMouseUp}
onContextMenu={(e) => e.evt.preventDefault()}
onTouchStart={onMouseDown}
onTouchMove={onMouseMove}
onTouchEnd={onMouseUp}
onDragEnd={onDragEnd}
onWheel={onWheel}>
<ReactReduxContext.Provider value={value}>
<Layer>
{pageRank.map((pageId) => (
<Page
key={pageId}
pageId={pageId}
isDraggable={isDraggable}
/>
))}
</Layer>
<LiveLayer />
</ReactReduxContext.Provider>
</Stage>
)}
</ReactReduxContext.Consumer>
</div>
</div>
)
Expand Down
11 changes: 8 additions & 3 deletions src/component/board/stroke.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function StrokeShape({ stroke, isDraggable }) {
x: e.target.attrs.x,
y: e.target.attrs.y,
id: stroke.id,
// pageId: stroke.pageId,
pageId: stroke.pageId,
})
)
}
Expand Down Expand Up @@ -145,8 +145,13 @@ export function StrokeShape({ stroke, isDraggable }) {
* Start the current stroke when mouse is pressed down
* @param {*} position
*/
export function startLiveStroke(position) {
store.dispatch(START_LIVESTROKE([position.x, position.y]))
export function startLiveStroke(position, pageId) {
store.dispatch(
START_LIVESTROKE({
pageId,
points: [position.x, position.y],
})
)
}

/**
Expand Down
17 changes: 6 additions & 11 deletions src/redux/slice/boardcontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const boardControlSlice = createSlice({
initialState: {
pageRank: [], // ["id1", "id2", ...]
pageCollection: {}, // {id1: canvasRef1, id2: canvasRef2, ...}
strokeCollection: {}, // FOR NOW WE CAN USE 1 COLLECTION
sessionID: "",
websocket: null,
},
Expand Down Expand Up @@ -52,25 +51,21 @@ const boardControlSlice = createSlice({
// Add stroke to collection
ADD_STROKE: (state, action) => {
const stroke = action.payload
const { id } = stroke
// state.pageCollection.strokes[id] = stroke
state.strokeCollection[id] = stroke
const { pageId, id } = stroke
state.pageCollection[pageId].strokes[id] = stroke
},

// Erase stroke from collection
ERASE_STROKE(state, action) {
const stroke = action.payload
// const { pageId, id } = stroke
// delete state.pageCollection[pageId].strokes[id]
delete state.strokeCollection[stroke.id]
const { pageId, id } = stroke
delete state.pageCollection[pageId].strokes[id]
},

// Update stroke position after dragging
UPDATE_STROKE(state, action) {
// const { x, y, id, pageId } = action.payload
// const stroke = state.pageCollection[pageId].strokes[id]
const { x, y, id } = action.payload
const stroke = state.strokeCollection[id]
const { x, y, id, pageId } = action.payload
const stroke = state.pageCollection[pageId].strokes[id]
stroke.x = x
stroke.y = y
},
Expand Down
6 changes: 5 additions & 1 deletion src/redux/slice/drawcontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const drawControlSlice = createSlice({
color: DEFAULT_COLOR,
width: DEFAULT_WIDTH * CANVAS_PIXEL_RATIO,
},
pageId: "",
points: [], // {"pageid": [x1,y1,x2,y2,...]}
x: 0, // be consistent with stroke description
y: 0,
Expand Down Expand Up @@ -63,14 +64,17 @@ const drawControlSlice = createSlice({
state.isMouseDown = isMouseDown
},
START_LIVESTROKE: (state, action) => {
const points = action.payload
const { pageId, points } = action.payload
state.liveStroke.pageId = pageId
state.liveStroke.points = points
},
// Update the current live stroke position
UPDATE_LIVESTROKE: (state, action) => {
const points = action.payload
state.liveStroke.points = [...state.liveStroke.points, ...points]
},
END_LIVESTROKE: (state) => {
state.liveStroke.pageId = ""
state.liveStroke.points = []
},
},
Expand Down

0 comments on commit 446e1c0

Please sign in to comment.