Skip to content
Open
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
8 changes: 5 additions & 3 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
"scripts": {
"preinstall": "python3 ../libs/build-libs.py && ./get_precache_files.py > src/extraPrecacheFiles.ts",
"prestart": "tsgo -p _build/tsconfig.json",
"start": "NODE_ENV='' node --experimental-import-meta-resolve _build/build.mjs",
"start": "cross-env NODE_ENV='' node --experimental-import-meta-resolve _build/build.mjs",
"prebuild": "tsgo -p _build/tsconfig.json",
"build": "NODE_ENV=production node --experimental-import-meta-resolve _build/build.mjs",
"test": "TZ=NZ jest",
"typecheck": "tsgo --noEmit --p ./tsconfig.json",
"typecheck:watch": "tsgo --noEmit --watch -p ./tsconfig.json",
"lint": "eslint --flag unstable_ts_config src/",
"lint:fix": "eslint --flag unstable_ts_config --fix src/"

},
"engines": {
"node": ">=17.1.0"
Expand Down Expand Up @@ -101,8 +102,10 @@
"@types/react-dom": "^17.0.25",
"@types/semver": "^7.5.8",
"@types/showdown": "^1.9.4",
"@typescript/native-preview": "7.0.0-dev.20250809.1",
"babel-eslint": "^10.1.0",
"browserslist": "^4.23.3",
"cross-env": "^10.1.0",
"esbuild": "~0.16.17",
"esbuild-plugin-browserslist": "~0.6.0",
"esbuild-plugin-manifest": "~0.5.0",
Expand All @@ -123,8 +126,7 @@
"servor": "^4.0.2",
"ts-jest": "^27.1.5",
"typescript-eslint": "^8.1.0",
"workbox-build": "^6.6.1",
"@typescript/native-preview": "7.0.0-dev.20250809.1"
"workbox-build": "^6.6.1"
},
"resolutions": {
"@types/react": "^17",
Expand Down
3 changes: 3 additions & 0 deletions app/src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@
export type ActionNames = keyof ActionFactories;

export type MicroPadActions = {
[ActionName in ActionNames]: ActionFactories[ActionName] extends ActionCreator<any>

Check warning on line 36 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type
? ReturnType<ActionFactories[ActionName]>
: ActionFactories[ActionName] extends AsyncActionCreators<any, any, any>

Check warning on line 38 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type

Check warning on line 38 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type

Check warning on line 38 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type
? { started: ReturnType<ActionFactories[ActionName]['started']>, done: ReturnType<ActionFactories[ActionName]['done']>, failed: ReturnType<ActionFactories[ActionName]['failed']> }
: never;
};

type ActionFactories = typeof actions;
type ActionTypes = {
[ActionName in ActionNames]: ActionFactories[ActionName] extends ActionCreator<any>

Check warning on line 45 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type
? ReturnType<ActionFactories[ActionName]>
: ActionFactories[ActionName] extends AsyncActionCreators<any, any, any>

Check warning on line 47 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type

Check warning on line 47 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type

Check warning on line 47 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type
? (ReturnType<ActionFactories[ActionName]['started']> | ReturnType<ActionFactories[ActionName]['done']> | ReturnType<ActionFactories[ActionName]['failed']>)
: never;
};
Expand All @@ -65,7 +65,7 @@
syncLogin: actionCreator.async<SyncLoginRequest, SyncUser, void>('SYNC_LOGIN'),
getSyncedNotepadList: actionCreator.async<SyncUser, CombinedNotepadSyncList, void>('SYNC_GET_NOTEPAD_LIST'),
syncDownload: actionCreator.async<string, Notepad, void>('SYNC_DOWNLOAD'),
syncUpload: actionCreator.async<SyncAction, void, any>('SYNC_UPLOAD'),

Check warning on line 68 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type
deleteFromSync: actionCreator.async<string, void, void>('SYNC_DELETE'),
addToSync: actionCreator.async<AddToSyncAction, string, void>('SYNC_CREATE'),
quickNote: actionCreator.async<void, string, void>('QUICK_NOTE'),
Expand Down Expand Up @@ -95,6 +95,9 @@
collapseAllExplorer: actionCreator<void>('COLLAPSE_ALL_EXPLORER'),
openEditor: actionCreator<string>('OPEN_EDITOR'),
updateElement: actionCreator<UpdateElementAction>('UPDATE_ELEMENT'),
addDrawing: actionCreator<any>('ADD_DRAWING'),

Check warning on line 98 in app/src/app/actions.ts

View workflow job for this annotation

GitHub Actions / Build, Lint & Test

Unexpected any. Specify a different type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: This action doesn't look like it is being dispatched anywhere? Was this part of an earlier approach that has since been replaced by the logic in the component?

undoDrawing: actionCreator<void>('UNDO_DRAWING'),
redoDrawing: actionCreator<void>('REDO_DRAWING'),
updateDefaultFontSize: actionCreator<string>('UPDATE_DEFAULT_FONT_SIZE'),
newSection: actionCreator<NewNotepadObjectAction>('NEW_SECTION'),
newNote: actionCreator<NewNotepadObjectAction>('NEW_NOTE'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,57 @@ export default class DrawingElementComponent extends React.Component<Props> {
private canvasImage?: Blob | null;

private rainbowIndex = 0;
private undoStack: string[] = [];
private redoStack: string[] = [];
private maxHistory = 50;

private pushUndo = () => {
if (!this.canvasElement) return;
const snapshot = this.canvasElement.toDataURL();
if (this.undoStack.length === 0 || this.undoStack[this.undoStack.length - 1] !== snapshot) {
this.undoStack.push(snapshot);
if (this.undoStack.length > this.maxHistory) this.undoStack.shift();
}
this.redoStack = [];
};


private undo = () => {
if (this.undoStack.length < 2 || !this.canvasElement) return;
const current = this.undoStack.pop(); // discard current state
this.redoStack.push(current!);
const prev = this.undoStack[this.undoStack.length - 1];
this.restoreCanvas(prev);
};

private redo = () => {
if (this.redoStack.length === 0 || !this.canvasElement) return;
const snapshot = this.redoStack.pop()!;
this.undoStack.push(snapshot);
this.restoreCanvas(snapshot);
};

private restoreCanvas = (snapshot: string) => {
if (!this.canvasElement) return;
const img = new Image();
img.onload = () => {
this.ctx.clearRect(0, 0, this.canvasElement!.width, this.canvasElement!.height);
this.ctx.drawImage(img, 0, 0);
};
img.src = snapshot;
}

private handleKeyDown = (e: KeyboardEvent) => {
if (e.ctrlKey && e.key.toLowerCase() === 'z') {
e.preventDefault();
this.undo();
} else if (e.ctrlKey && e.key.toLowerCase() === 'y') {
e.preventDefault();
this.redo();
}
};



render() {
const { element, elementEditing, theme } = this.props;
Expand Down Expand Up @@ -146,31 +197,38 @@ export default class DrawingElementComponent extends React.Component<Props> {

componentDidMount() {
this.componentDidUpdate();
window.addEventListener('keydown', this.handleKeyDown, true);
};

componentWillUnmount() {
window.removeEventListener('keydown', this.handleKeyDown);
}


componentDidUpdate(prevProps?: Readonly<Props>) {
const { element, noteAssets } = this.props;

this.ongoingTouches = new OngoingTouches();

const canvasElement = this.canvasElement;
if (!!canvasElement) {
setTimeout(() => this.initCanvas(), 0);

// setTimeout(() => this.initCanvas(), 0);
this.initCanvas();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: This was intentionally in a 0ms setTimeout to ensure the init canvas happened on the next cycle of the event loop (and thus outside of the context of a componentDidUpdate). It's been many years since I wrote a lot of this code-- can you please ensure that doing the init right here is okay?

const isNewEditor = this.props.elementEditing !== prevProps?.elementEditing;
if (isNewEditor) {
// Restore saved image to canvas
const img: HTMLImageElement = new Image();
img.onload = () => {
const canvasElement = this.canvasElement;
if (!canvasElement) return;
canvasElement.width = img.naturalWidth;
canvasElement.height = img.naturalHeight;
this.ctx.drawImage(img, 0, 0);
};
img.src = noteAssets[element.args.ext!];
}
return;
const img: HTMLImageElement = new Image();
img.onload = () => {
const canvasElement = this.canvasElement;
if (!canvasElement) return;
canvasElement.width = img.naturalWidth;
canvasElement.height = img.naturalHeight;
this.ctx.drawImage(img, 0, 0);
};
img.src = noteAssets[element.args.ext!];
}
return;
}

let img: HTMLImageElement = new Image();
img.onload = () => {
Expand Down Expand Up @@ -215,6 +273,7 @@ export default class DrawingElementComponent extends React.Component<Props> {
}

this.ctx = canvasElement.getContext('2d')!;
this.pushUndo();

canvasElement.onpointerdown = event => {
this.ongoingTouches.setTouch(event);
Expand Down Expand Up @@ -267,6 +326,7 @@ export default class DrawingElementComponent extends React.Component<Props> {
this.ctx.lineTo(pos.x, pos.y);

this.ongoingTouches.deleteTouch(event.pointerId);
this.pushUndo();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: indentation

};

canvasElement.onpointercancel = event => {
Expand Down Expand Up @@ -373,4 +433,4 @@ class OngoingTouches {
deleteTouch(id: number) {
this.touches[id] = null;
}
}
}
88 changes: 55 additions & 33 deletions app/src/app/reducers/EditorReducer.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,63 @@
import { actions } from '../actions';
import { createSlice, SliceCaseReducers } from '@reduxjs/toolkit';

export type EditorState = {
shouldSpellCheck: boolean,
shouldWordWrap: boolean,
drawMode: DrawMode,
drawingLineColour: string
};

export const enum DrawMode {
Line = 'line',
ERASE = 'erase',
RAINBOW = 'rainbow'
Line = 'line',
ERASE = 'erase',
RAINBOW = 'rainbow',
}

export type EditorState = {
shouldSpellCheck: boolean;
shouldWordWrap: boolean;
drawMode: DrawMode;
drawingLineColour: string;
drawings: any[];
past: any[];
future: any[];
};

export const editorSlice = createSlice<EditorState, SliceCaseReducers<EditorState>, 'editor'>({
name: 'editor',
initialState: {
shouldSpellCheck: true,
shouldWordWrap: true,
drawMode: DrawMode.Line,
drawingLineColour: '#000000'
},
reducers: {},
extraReducers: builder => builder
.addCase(actions.toggleSpellCheck, (state, action) => ({
...state,
shouldSpellCheck: action.payload ?? !state.shouldSpellCheck
}))
.addCase(actions.toggleWordWrap, (state, action) => ({
...state,
shouldWordWrap: action.payload ?? !state.shouldWordWrap
}))
.addCase(actions.setDrawMode, (state, { payload }) => ({ ...state, drawMode: payload }))
.addCase(actions.setDrawingLineColour, (state, { payload }) => ({
...state,
drawingLineColour: payload,
drawMode: DrawMode.Line
}))
name: 'editor',
initialState: {
shouldSpellCheck: true,
shouldWordWrap: true,
drawMode: DrawMode.Line,
drawingLineColour: '#000000',
drawings: [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (non-blocking): These are all related (as is drawMode, the line colour, etc.) I wonder if it's worth making a drawingState: { /* etc. */ } in here. It's fine as-is though so up to you (although I might suggest prefixing past and future to be like drawingPast and drawingFuture)

past: [],
future: [],
},
reducers: {},
extraReducers: builder => builder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Reducers must be immutable, so directly mutating state in these cases is not allowed. Please do immutable updates like was done before. If you want to do in-place array manipulation with push/pop please clone the arrays with ... first.

.addCase(actions.toggleSpellCheck, (state, action) => {
state.shouldSpellCheck = action.payload ?? !state.shouldSpellCheck;
})
.addCase(actions.toggleWordWrap, (state, action) => {
state.shouldWordWrap = action.payload ?? !state.shouldWordWrap;
})
.addCase(actions.setDrawMode, (state, { payload }) => {
state.drawMode = payload;
})
.addCase(actions.setDrawingLineColour, (state, { payload }) => {
state.drawingLineColour = payload;
state.drawMode = DrawMode.Line;
})
.addCase(actions.addDrawing, (state, { payload }) => {
state.past.push([...state.drawings]); // push current drawings to past
state.future = [];
state.drawings.push(payload);
})
.addCase(actions.undoDrawing, (state) => {
if (state.past.length === 0) return;
const previous = state.past.pop()!;
state.future.unshift([...state.drawings]);
state.drawings = previous;
})
.addCase(actions.redoDrawing, (state) => {
if (state.future.length === 0) return;
const next = state.future.shift()!;
state.past.push([...state.drawings]);
state.drawings = next;
}),
});
3 changes: 1 addition & 2 deletions app/src/app/reducers/NoteReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class NoteReducer extends AbstractReducer<ICurrentNoteState> {
x: 0,
y: 0,
enabled: false
}
},
};

public reducer(state: ICurrentNoteState | undefined, action: Action): ICurrentNoteState {
Expand Down Expand Up @@ -95,7 +95,6 @@ export class NoteReducer extends AbstractReducer<ICurrentNoteState> {
insertElement: { ...state.insertElement, enabled: false }
}
}

return state;
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/app/services/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ export function enableKeyboardShortcuts(store: Store<IStoreState, MicroPadAction
store.dispatch(actions.quickNotepad());
}
});

mousetrap.bind('mod+z', e => {
e.preventDefault();
store.dispatch(actions.undoDrawing());
});

mousetrap.bind('mod+y', e => {
e.preventDefault();
store.dispatch(actions.redoDrawing());
});
}
13 changes: 13 additions & 0 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,11 @@
resolved "https://registry.yarnpkg.com/@emotion/weak-memoize/-/weak-memoize-0.4.0.tgz#5e13fac887f08c44f76b0ccaf3370eb00fec9bb6"
integrity sha512-snKqtPW01tN0ui7yu9rGv69aJXr/a/Ywvl11sUjNtEcRc+ng/mQriFL0wLXMef74iHa/EkftbDzU9F8iFbH+zg==

"@epic-web/invariant@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@epic-web/invariant/-/invariant-1.0.0.tgz#1073e5dee6dd540410784990eb73e4acd25c9813"
integrity sha512-lrTPqgvfFQtR/eY/qkIzp98OGdNJu0m5ji3q/nJI8v3SXkRKEnWiOxMmbvcSoAIzv/cGiuvRy57k4suKQSAdwA==

"@esbuild/[email protected]":
version "0.16.17"
resolved "https://registry.yarnpkg.com/@esbuild/android-arm64/-/android-arm64-0.16.17.tgz#cf91e86df127aa3d141744edafcba0abdc577d23"
Expand Down Expand Up @@ -3109,6 +3114,14 @@ cosmiconfig@^7.0.0:
path-type "^4.0.0"
yaml "^1.10.0"

cross-env@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/cross-env/-/cross-env-10.1.0.tgz#cfd2a6200df9ed75bfb9cb3d7ce609c13ea21783"
integrity sha512-GsYosgnACZTADcmEyJctkJIoqAhHjttw7RsFrVoJNXbsWWqaq6Ym+7kZjq6mS45O0jij6vtiReppKQEtqWy6Dw==
dependencies:
"@epic-web/invariant" "^1.0.0"
cross-spawn "^7.0.6"

cross-spawn@^7.0.0, cross-spawn@^7.0.3, cross-spawn@^7.0.6:
version "7.0.6"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.6.tgz#8a58fe78f00dcd70c370451759dfbfaf03e8ee9f"
Expand Down