Skip to content

Commit

Permalink
feat: allow readonly actions to be used in viewMode (excalidraw#5982)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle authored Dec 11, 2022
1 parent 8466eb0 commit cc9e764
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/actions/actionCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const actionClearCanvas = register({

export const actionZoomIn = register({
name: "zoomIn",
viewMode: true,
trackEvent: { category: "canvas" },
perform: (_elements, appState, _, app) => {
return {
Expand Down Expand Up @@ -126,6 +127,7 @@ export const actionZoomIn = register({

export const actionZoomOut = register({
name: "zoomOut",
viewMode: true,
trackEvent: { category: "canvas" },
perform: (_elements, appState, _, app) => {
return {
Expand Down Expand Up @@ -162,6 +164,7 @@ export const actionZoomOut = register({

export const actionResetZoom = register({
name: "resetZoom",
viewMode: true,
trackEvent: { category: "canvas" },
perform: (_elements, appState, _, app) => {
return {
Expand Down Expand Up @@ -271,6 +274,7 @@ export const actionZoomToSelected = register({

export const actionZoomToFit = register({
name: "zoomToFit",
viewMode: true,
trackEvent: { category: "canvas" },
perform: (elements, appState) => zoomToFitElements(elements, appState, false),
keyTest: (event) =>
Expand All @@ -282,6 +286,7 @@ export const actionZoomToFit = register({

export const actionToggleTheme = register({
name: "toggleTheme",
viewMode: true,
trackEvent: { category: "canvas" },
perform: (_, appState, value) => {
return {
Expand Down
1 change: 1 addition & 0 deletions src/actions/actionExport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export const actionSaveToActiveFile = register({

export const actionSaveFileToDisk = register({
name: "saveFileToDisk",
viewMode: true,
trackEvent: { category: "export" },
perform: async (elements, appState, value, app) => {
try {
Expand Down
2 changes: 2 additions & 0 deletions src/actions/actionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const actionToggleEditMenu = register({

export const actionFullScreen = register({
name: "toggleFullScreen",
viewMode: true,
trackEvent: { category: "canvas", predicate: (appState) => !isFullScreen() },
perform: () => {
if (!isFullScreen()) {
Expand All @@ -73,6 +74,7 @@ export const actionFullScreen = register({

export const actionShortcuts = register({
name: "toggleShortcuts",
viewMode: true,
trackEvent: { category: "menu", action: "toggleHelpDialog" },
perform: (_elements, appState, _, { focusContainer }) => {
if (appState.openDialog === "help") {
Expand Down
1 change: 1 addition & 0 deletions src/actions/actionNavigate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { register } from "./register";

export const actionGoToCollaborator = register({
name: "goToCollaborator",
viewMode: true,
trackEvent: { category: "collab" },
perform: (_elements, appState, value) => {
const point = value as Collaborator["pointer"];
Expand Down
1 change: 1 addition & 0 deletions src/actions/actionToggleGridMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AppState } from "../types";

export const actionToggleGridMode = register({
name: "gridMode",
viewMode: true,
trackEvent: {
category: "canvas",
predicate: (appState) => !appState.gridSize,
Expand Down
1 change: 1 addition & 0 deletions src/actions/actionToggleStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CODES, KEYS } from "../keys";

export const actionToggleStats = register({
name: "stats",
viewMode: true,
trackEvent: { category: "menu" },
perform(elements, appState) {
return {
Expand Down
1 change: 1 addition & 0 deletions src/actions/actionToggleViewMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { register } from "./register";

export const actionToggleViewMode = register({
name: "viewMode",
viewMode: true,
trackEvent: {
category: "canvas",
predicate: (appState) => !appState.viewModeEnabled,
Expand Down
1 change: 1 addition & 0 deletions src/actions/actionToggleZenMode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { register } from "./register";

export const actionToggleZenMode = register({
name: "zenMode",
viewMode: true,
trackEvent: {
category: "canvas",
predicate: (appState) => !appState.zenModeEnabled,
Expand Down
8 changes: 2 additions & 6 deletions src/actions/manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from "./types";
import { ExcalidrawElement } from "../element/types";
import { AppClassProperties, AppState } from "../types";
import { MODES } from "../constants";
import { trackEvent } from "../analytics";

const trackAction = (
Expand Down Expand Up @@ -103,11 +102,8 @@ export class ActionManager {

const action = data[0];

const { viewModeEnabled } = this.getAppState();
if (viewModeEnabled) {
if (!Object.values(MODES).includes(data[0].name)) {
return false;
}
if (this.getAppState().viewModeEnabled && action.viewMode !== true) {
return false;
}

const elements = this.getElementsIncludingDeleted();
Expand Down
3 changes: 3 additions & 0 deletions src/actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,7 @@ export interface Action {
value: any,
) => boolean;
};
/** if set to `true`, allow action to be performed in viewMode.
* Defaults to `false` */
viewMode?: boolean;
}
6 changes: 0 additions & 6 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ export const IDLE_THRESHOLD = 60_000;
// Report a user active each ACTIVE_THRESHOLD milliseconds
export const ACTIVE_THRESHOLD = 3_000;

export const MODES = {
VIEW: "viewMode",
ZEN: "zenMode",
GRID: "gridMode",
};

export const THEME_FILTER = cssVariables.themeFilter;

export const URL_QUERY_KEYS = {
Expand Down

0 comments on commit cc9e764

Please sign in to comment.