Skip to content

Commit

Permalink
Create board clone on the server
Browse files Browse the repository at this point in the history
Also adds support for CRDTs in creating with templateId, as cloning now
uses the same mechanism
  • Loading branch information
raimohanska committed Mar 11, 2024
1 parent bc0d831 commit e039fd8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 31 deletions.
9 changes: 8 additions & 1 deletion backend/src/common-event-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { AppEvent, Board, checkBoardAccess, defaultBoardSize } from "../../common/src/domain"
import * as Y from "yjs"
import { AppEvent, Board, CrdtEnabled, checkBoardAccess, defaultBoardSize } from "../../common/src/domain"
import { addBoard } from "./board-state"
import { fetchBoard } from "./board-store"
import { yWebSocketServer } from "./board-yjs-server"
import { MessageHandlerResult } from "./connection-handler"
import { getAuthenticatedUserFromJWT } from "./http-session"
import {
Expand Down Expand Up @@ -94,6 +96,11 @@ export async function handleCommonEvent(socket: WsWrapper, appEvent: AppEvent):
}
}
const board = { ...defaultBoardSize, items: {}, connections: [], ...template, ...payload, serial: 0 }
if (template && template.crdt === CrdtEnabled) {
const templateDoc = await yWebSocketServer.docs.getYDocAndWaitForFetch(template.id)
const newDoc = await yWebSocketServer.docs.getYDocAndWaitForFetch(board.id)
Y.applyUpdate(newDoc, Y.encodeStateAsUpdate(templateDoc))
}
await addBoard(board)
socket.send(toBuffer({ action: "board.add.ack", boardId: board.id }))
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/board/header/BoardViewHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { SharingModalDialog } from "./SharingModalDialog"
import { UserInfoView } from "./UserInfoView"
import { Rect } from "../../../../common/src/geometry"
import { CRDTStore } from "../../store/crdt-store"
import * as uuid from "uuid"

export function BoardViewHeader({
usersOnBoard,
Expand Down Expand Up @@ -44,8 +45,9 @@ export function BoardViewHeader({
const navigator = getNavigator<Routes>()
function makeCopy() {
const newBoard = {
...crdtStore.cloneBoard(board.get()),
id: uuid.v4(),
name: `${nameAtom.get()} copy`,
templateId: board.get()!.id,
accessPolicy: defaultAccessPolicy(sessionState.get(), false),
}
createBoardAndNavigate(newBoard, dispatch, navigator, eventsFromServer)
Expand Down
31 changes: 2 additions & 29 deletions frontend/src/store/crdt-store.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import * as L from "lonna"
import * as uuid from "uuid"
import { IndexeddbPersistence } from "y-indexeddb"
import { WebsocketProvider } from "y-websocket"
import * as Y from "yjs"
import { Board, Id, Item, PersistableBoardItemEvent } from "../../../common/src/domain"
import {
augmentBoardWithCRDT,
augmentItemsWithCRDT,
getCRDTField,
importItemsIntoCRDT,
} from "../../../common/src/board-crdt-helper"
import { augmentItemsWithCRDT, getCRDTField, importItemsIntoCRDT } from "../../../common/src/board-crdt-helper"
import { Id, Item, PersistableBoardItemEvent } from "../../../common/src/domain"
import { getWebSocketRootUrl } from "./server-connection"

type BoardCRDT = ReturnType<typeof BoardCRDT>
Expand Down Expand Up @@ -123,29 +117,8 @@ export function CRDTStore(
return boardCrdt.augmentItems(items)
}

function cloneBoard(board: Board): Board {
const newId = uuid.v4()
if (!boardCrdt || boardCrdt.boardId !== board.id) {
return {
...board,
id: newId,
}
}

const newBoard = {
...augmentBoardWithCRDT(boardCrdt.doc, board),
id: newId,
}

const temporaryBoardCrdt = BoardCRDT(newId, online, localBoardItemEvents, getSocketRoot, WebSocketPolyfill)
importItemsIntoCRDT(temporaryBoardCrdt.doc, Object.values(newBoard.items))
temporaryBoardCrdt.disconnect()
return newBoard
}

return {
getBoardCrdt,
augmentItems,
cloneBoard,
}
}
1 change: 1 addition & 0 deletions playwright/src/tests/board.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ test.describe("Basic board functionality", () => {
testWithBothBoardTypes("Clone the board", async ({ board, page }) => {
const semigroups = await board.createArea(500, 200, "Semigroups")
const functors = await board.createNoteWithText(200, 200, "Functors")
await sleep(1000)
await board.cloneBoard()
await board.assertBoardName("Clone the board copy")
await expect(semigroups).toBeVisible()
Expand Down

0 comments on commit e039fd8

Please sign in to comment.