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

Fix #1585: DarkColorHandler need to remember used color across editor session #1587

Merged
merged 1 commit into from
Feb 22, 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
1 change: 1 addition & 0 deletions demo/scripts/controls/sidePane/snapshot/SnapshotPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class SnapshotPane extends React.Component<SnapshotPaneProps, Sna
this.props.onRestoreSnapshot({
html: this.textarea.value,
metadata: null,
knownColors: [],
})
}>
{'Restore snapshot'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function addUndoSnapshotInternal(core: EditorCore, canUndoByBackspace: boolean)
{
html: core.contentDiv.innerHTML,
metadata,
knownColors: core.darkColorHandler?.getKnownColorsCopy() || [],
},
canUndoByBackspace
);
Expand Down
13 changes: 13 additions & 0 deletions packages/roosterjs-editor-core/lib/coreApi/restoreUndoSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ export const restoreUndoSnapshot: RestoreUndoSnapshot = (core: EditorCore, step:
true /*triggerContentChangedEvent*/,
snapshot.metadata ?? undefined
);

const darkColorHandler = core.darkColorHandler;
const isDarkModel = core.lifecycle.isDarkMode;

if (darkColorHandler) {
snapshot.knownColors.forEach(color => {
darkColorHandler.registerColor(
color.lightModeColor,
isDarkModel,
color.darkModeColor
);
});
}
} finally {
core.undo.isRestoring = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function createUndoSnapshotServiceBridge(
? {
canMove: (delta: number) => service.canMove(delta),
move: (delta: number): Snapshot | null =>
(html = service.move(delta)) ? { html, metadata: null } : null,
(html = service.move(delta)) ? { html, metadata: null, knownColors: [] } : null,
addSnapshot: (snapshot: Snapshot, isAutoCompleteSnapshot: boolean) =>
service.addSnapshot(
snapshot.html +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ const COLOR_VAR_PREFIX = 'darkColor';
* @internal
*/
export default class DarkColorHandlerImpl implements DarkColorHandler {
private knownColors: Record<string, ModeIndependentColor> = {};
private knownColors: Record<string, Readonly<ModeIndependentColor>> = {};

constructor(private contentDiv: HTMLElement, private getDarkColor: (color: string) => string) {}

/**
* Get a copy of known colors
* @returns
*/
getKnownColorsCopy() {
return Object.values(this.knownColors);
}

/**
* Given a light mode color value and an optional dark mode color value, register this color
* so that editor can handle it, then return the CSS color value for current color mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('addUndoSnapshot', () => {
start: [],
end: [],
},
knownColors: [],
},
false
);
Expand All @@ -54,6 +55,7 @@ describe('addUndoSnapshot', () => {
start: [],
end: [],
},
knownColors: [],
},
false
);
Expand Down Expand Up @@ -94,10 +96,12 @@ describe('addUndoSnapshot', () => {
expect(snapshot1).toEqual({
html: 'result 1',
metadata: { type: 0, isDarkMode: false, start: [], end: [] },
knownColors: [],
});
expect(snapshot2).toEqual({
html: 'result 2',
metadata: { type: 0, isDarkMode: false, start: [], end: [] },
knownColors: [],
});
expect(core.undo.isNested).toBeFalsy();
});
Expand Down Expand Up @@ -259,6 +263,7 @@ describe('addUndoSnapshot', () => {
start: [],
end: [],
},
knownColors: [],
},
false
);
Expand Down Expand Up @@ -293,6 +298,7 @@ describe('addUndoSnapshot', () => {
isDarkMode: false,
...selectionPath,
},
knownColors: [],
},
false
);
Expand Down Expand Up @@ -329,6 +335,7 @@ describe('addUndoSnapshot', () => {
isDarkMode: false,
...coordinates,
},
knownColors: [],
},
false
);
Expand Down
108 changes: 86 additions & 22 deletions packages/roosterjs-editor-core/test/corePlugins/undoPluginTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,24 +444,54 @@ describe('UndoPlugin', () => {
});

it('can undo autoComplete', () => {
state.snapshotsService.addSnapshot({ html: 'snapshot 1', metadata: null }, false);
state.snapshotsService.addSnapshot({ html: 'snapshot 2', metadata: null }, true);
state.snapshotsService.addSnapshot({ html: 'snapshot 3', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 1', metadata: null, knownColors: [] },
false
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 2', metadata: null, knownColors: [] },
true
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 3', metadata: null, knownColors: [] },
false
);
expect(state.snapshotsService.canUndoAutoComplete()).toBeTrue();
});

it('cannot undo autoComplete', () => {
state.snapshotsService.addSnapshot({ html: 'snapshot 1', metadata: null }, false);
state.snapshotsService.addSnapshot({ html: 'snapshot 2', metadata: null }, true);
state.snapshotsService.addSnapshot({ html: 'snapshot 3', metadata: null }, false);
state.snapshotsService.addSnapshot({ html: 'snapshot 4', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 1', metadata: null, knownColors: [] },
false
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 2', metadata: null, knownColors: [] },
true
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 3', metadata: null, knownColors: [] },
false
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 4', metadata: null, knownColors: [] },
false
);
expect(state.snapshotsService.canUndoAutoComplete()).toBeFalse();
});

it('Backspace trigger undo when can undo autoComplete', () => {
state.snapshotsService.addSnapshot({ html: 'snapshot 1', metadata: null }, false);
state.snapshotsService.addSnapshot({ html: 'snapshot 2', metadata: null }, true);
state.snapshotsService.addSnapshot({ html: 'snapshot 3', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 1', metadata: null, knownColors: [] },
false
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 2', metadata: null, knownColors: [] },
true
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 3', metadata: null, knownColors: [] },
false
);

const undo = jasmine.createSpy('undo');
const preventDefault = jasmine.createSpy('preventDefault');
Expand All @@ -486,9 +516,18 @@ describe('UndoPlugin', () => {
});

it('Other key does not trigger undo auto complete', () => {
state.snapshotsService.addSnapshot({ html: 'snapshot 1', metadata: null }, false);
state.snapshotsService.addSnapshot({ html: 'snapshot 2', metadata: null }, true);
state.snapshotsService.addSnapshot({ html: 'snapshot 3', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 1', metadata: null, knownColors: [] },
false
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 2', metadata: null, knownColors: [] },
true
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 3', metadata: null, knownColors: [] },
false
);

const undo = jasmine.createSpy('undo');
const preventDefault = jasmine.createSpy('preventDefault');
Expand All @@ -514,9 +553,18 @@ describe('UndoPlugin', () => {
});

it('Another undo snapshot is added, cannot undo autocomplete any more', () => {
state.snapshotsService.addSnapshot({ html: 'snapshot 1', metadata: null }, false);
state.snapshotsService.addSnapshot({ html: 'snapshot 2', metadata: null }, true);
state.snapshotsService.addSnapshot({ html: 'snapshot 3', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 1', metadata: null, knownColors: [] },
false
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 2', metadata: null, knownColors: [] },
true
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 3', metadata: null, knownColors: [] },
false
);

const undo = jasmine.createSpy('undo');
const preventDefault = jasmine.createSpy('preventDefault');
Expand All @@ -526,7 +574,10 @@ describe('UndoPlugin', () => {
editor.getSelectionRange = () => range;
editor.getFocusedPosition = () => pos;
editor.addUndoSnapshot = () =>
state.snapshotsService.addSnapshot({ html: 'snapshot 4', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 4', metadata: null, knownColors: [] },
false
);
state.autoCompletePosition = pos;

plugin.onPluginEvent({
Expand All @@ -545,7 +596,10 @@ describe('UndoPlugin', () => {
});

it('Position changed, cannot undo autocomplete for Backspace', () => {
state.snapshotsService.addSnapshot({ html: 'snapshot 1', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 1', metadata: null, knownColors: [] },
false
);

const undo = jasmine.createSpy('undo');
const preventDefault = jasmine.createSpy('preventDefault');
Expand All @@ -559,7 +613,10 @@ describe('UndoPlugin', () => {

editor.getFocusedPosition = () => pos2;
editor.addUndoSnapshot = () =>
state.snapshotsService.addSnapshot({ html: 'snapshot 4', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 4', metadata: null, knownColors: [] },
false
);

// Press backspace first time, to let plugin remember last pressed key
plugin.onPluginEvent({
Expand All @@ -570,8 +627,14 @@ describe('UndoPlugin', () => {
}),
});

state.snapshotsService.addSnapshot({ html: 'snapshot 2', metadata: null }, true);
state.snapshotsService.addSnapshot({ html: 'snapshot 3', metadata: null }, false);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 2', metadata: null, knownColors: [] },
true
);
state.snapshotsService.addSnapshot(
{ html: 'snapshot 3', metadata: null, knownColors: [] },
false
);
state.autoCompletePosition = pos;

plugin.onPluginEvent({
Expand Down Expand Up @@ -611,6 +674,7 @@ describe('UndoPlugin', () => {
start: [1],
end: [2],
},
knownColors: [],
},
false
);
Expand All @@ -627,7 +691,7 @@ describe('UndoPlugin', () => {
expect(canUndoAutoComplete).toHaveBeenCalled();

expect(canMoveResult).toBe(true);
expect(moveResult).toEqual({ html: 'test', metadata: null });
expect(moveResult).toEqual({ html: 'test', metadata: null, knownColors: [] });
expect(canUndoAutoCompleteResult).toBe(true);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ModeIndependentColor from './ModeIndependentColor';

/**
* Represents a combination of color key, light color and dark color, parsed from existing color value
*/
Expand Down Expand Up @@ -42,4 +44,9 @@ export default interface DarkColorHandler {
* @param color The color string to parse
*/
parseColorValue(color: string | null | undefined): ColorKeyAndValue;

/**
* Get a copy of known colors
*/
getKnownColorsCopy(): Readonly<ModeIndependentColor>[];
}
6 changes: 6 additions & 0 deletions packages/roosterjs-editor-types/lib/interface/Snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ModeIndependentColor from './ModeIndependentColor';
import { ContentMetadata } from './ContentMetadata';

/**
Expand All @@ -13,4 +14,9 @@ export default interface Snapshot {
* Metadata of the editor content state
*/
metadata: ContentMetadata | null;

/**
* Known colors for dark mode
*/
knownColors: Readonly<ModeIndependentColor>[];
}