Skip to content
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

refactor(service-providers): remember tool states like stroke width #1962

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
## [2.2.3](https://github.com/netless-io/flat/compare/v2.2.2...v2.2.3) (2023-07-03)


### Features

* **project**: add replay room url protocol ([#1955](https://github.com/netless-io/flat/issues/1955)) ([4d628a46](https://github.com/netless-io/flat/commit/4d628a4679e35f9b8531b09110762cfdf73c865e))

### Bug Fixes

* **flat-components**: fix login cover image ([#1957](https://github.com/netless-io/flat/issues/1957)) ([96153039](https://github.com/netless-io/flat/commit/9615303921bfa0c5fd72d2dfd15fa6bb7c270832))
* **flat-stores**: remove legacy code of scroll to top ([#1953](https://github.com/netless-io/flat/issues/1953)) ([40759210](https://github.com/netless-io/flat/commit/40759210f598ffd0c62d700456898c26b1c840d6))

### Performance Improvements

* **flat-stores**: fetch users info only when necessary ([#1946](https://github.com/netless-io/flat/issues/1946)) ([d93ecc1b](https://github.com/netless-io/flat/commit/d93ecc1b3c517eb247dce276c9a4600a28a442a9))



## [2.2.2](https://github.com/netless-io/flat/compare/v2.2.1...v2.2.2) (2023-05-16)


Expand Down
68 changes: 58 additions & 10 deletions service-providers/fastboard/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createFastboard, createUI, FastboardApp, addManagerListener } from "@netless/fastboard";
import { DeviceType, RoomPhase, DefaultHotKeys } from "white-web-sdk";
import type { Camera } from "white-web-sdk";
import type { Camera, MemberState } from "white-web-sdk";
import type { FlatI18n } from "@netless/flat-i18n";
import {
IServiceWhiteboard,
Expand Down Expand Up @@ -203,10 +203,7 @@ export class Fastboard extends IServiceWhiteboard {
userPayload: {
uid,
nickName,
// @deprecated
userId: uid,
// @deprecated
cursorName: nickName,
// avatar: "url/to/avatar.png",
},
isWritable: this.isWritable,
uid,
Expand All @@ -225,7 +222,7 @@ export class Fastboard extends IServiceWhiteboard {
changeToStraight: "l",
changeToArrow: "a",
changeToHand: "h",
// disable builtin copy-paste hotkeys
// disable builtin copy-paste hotkeys, they are used to paste images from user clipboard
copy: undefined,
paste: undefined,
},
Expand Down Expand Up @@ -298,21 +295,39 @@ export class Fastboard extends IServiceWhiteboard {
);

// enable "text select text" on writable, once
// set "text size = 36", once
const disposeInitToolsId = "init-tools";
this.sideEffect.push(
fastboardAPP.writable.subscribe(writable => {
if (writable) {
fastboardAPP.toggleTextCanSelectText(true);
if (fastboardAPP.memberState.value.textSize === 16) {
fastboardAPP.setTextSize(36);
}
this.sideEffect.flush(disposeInitToolsId);
}
}),
disposeInitToolsId,
);

// restore and listen to appliance settings (i.e. memberState) on writable
const disposeMemberStateId = "member-state";
this.sideEffect.push(
fastboardAPP.writable.subscribe(writable => {
if (writable) {
initMemberState(fastboardAPP);
// 'subscribe' executes immediately, at which time the effect
// is not yet pushed into the side effect manager, so we need to
// wait for the next tick to dispose or replace it.
Promise.resolve().then(() => {
// using the same disposeId here, which means the old effect ('subscribe' above)
// will be disposed (removed) first, then the new effect ('subscribe' below) will take place.
this.sideEffect.push(
fastboardAPP.memberState.subscribe(saveMemberState),
disposeMemberStateId,
);
});
}
}),
disposeMemberStateId,
);

// reset scroll position when page changed
this.sideEffect.push(
fastboardAPP.pageIndex.reaction(() => {
Expand Down Expand Up @@ -456,3 +471,36 @@ function convertRoomPhase(roomPhase: RoomPhase): IServiceWhiteboardPhase {
}
}
}

const RememberStates: Array<keyof MemberState> = [
"strokeColor",
"strokeWidth",
"textColor",
"textSize",
"pencilEraserSize",
];

function initMemberState(app: FastboardApp): void {
const raw = localStorage.getItem("FastboardMemberState") || "{}";
let memberState: Partial<MemberState> = {};
try {
memberState = JSON.parse(raw) || {};
} catch {
// ignore
}
// The text size is 16 by default, which is too small at the first sight, change it to 36.
// Some users may still want to use 16, in that case we have no way to know the purpose.
if (!memberState.textSize || memberState.textSize === 16) {
memberState.textSize = 36;
}
// It is safe to call set state here since fastboard is writable now.
app.room.setMemberState(memberState);
}

function saveMemberState(memberState: MemberState): void {
const filtered: Record<string, any> = Object.create(null);
for (const key of RememberStates) {
filtered[key] = memberState[key];
}
localStorage.setItem("FastboardMemberState", JSON.stringify(filtered));
}