Skip to content

History extension #77

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

Merged
merged 6 commits into from
Feb 24, 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: 0 additions & 1 deletion .vercelignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
yarn.lock
14 changes: 7 additions & 7 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
"@harlem/extension-trace": "^3.0.0",
"@harlem/extension-transaction": "^3.0.0",
"date-fns": "^2.29.3",
"date-fns-tz": "^1.3.7",
"date-fns-tz": "^2.0.0",
"flex-layout-attribute": "^1.0.3",
"harlem": "^3.0.0",
"vue": "^3.2.45"
"vue": "^3.2.47"
},
"devDependencies": {
"@vitejs/plugin-vue": "^3.2.0",
"@vue/compiler-sfc": "^3.2.45",
"rollup-plugin-visualizer": "^5.8.3",
"sass": "^1.56.1",
"vite": "^3.2.5"
"@vitejs/plugin-vue": "^4.0.0",
"@vue/compiler-sfc": "^3.2.47",
"rollup-plugin-visualizer": "^5.9.0",
"sass": "^1.58.3",
"vite": "^4.1.4"
}
}
37 changes: 23 additions & 14 deletions app/src/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@
<br>
If you have the Vue <strong>devtools</strong> installed, open them up and change the inspector to Harlem to see the store. The source code for this demo is available <a href="https://github.com/andrewcourtice/harlem/tree/main/app" target="_blank" rel="noopener noreferrer">here</a>.
</p>
<choice-group class="app__theme">
<choice v-for="{ label, value } in state.themes"
:key="value"
v-model="theme"
:id="value"
:value="value">
{{ label }}
</choice>
</choice-group>
<div class="app__options" layout="rows center-justify gap-small">
<choice-group self="sm-full">
<choice v-for="{ label, value } in state.themes"
:key="value"
v-model="theme"
:id="value"
:value="value">
{{ label }}
</choice>
</choice-group>
<div layout="rows center-right gap-x-small" self="sm-full">
<button class="button" self="sm-half" :disabled="!canUndo()" @click="undo()">Undo</button>
<button class="button" self="sm-half" :disabled="!canRedo()" @click="redo()">Redo</button>
</div>
</div>
</header>
<div class="app__options" layout="rows center-justify">
<div class="app__actions" layout="rows center-justify gap-small">
<choice-group self="sm-full">
<choice v-for="{ label, value } in state.clockTypes"
:key="value"
Expand Down Expand Up @@ -81,11 +87,15 @@ import {
} from 'date-fns-tz';

import {
canRedo,
canUndo,
clocks,
computeState,
loadTimezones,
redo,
removeClock,
state,
undo,
} from './stores/app';

loadTimezones();
Expand All @@ -95,7 +105,7 @@ watchEffect(() => document.body.setAttribute('theme', state.theme.toLowerCase())
const addClockModal = ref();

const theme = computeState(state => state.theme);
const clockType = computeState(state => state.clockType);
const clockType = computeState(state => state.clockType, 'change-clock-type');

const clockComponent = computed(() => state.clockType === 'analogue'
? AnalogueClock
Expand Down Expand Up @@ -140,13 +150,12 @@ function openAddClockModal() {
text-align: center;
}

.app__theme {
.app__options {
margin-top: 2rem;
}

.app__options {
.app__actions {
margin: 2rem 0;
gap: 1rem;
}

.app__clocks {
Expand Down
24 changes: 24 additions & 0 deletions app/src/assets/styles/_layout.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
[self="size-auto"] {
width: auto;
}

[layout~="gap-none"] {
gap: 0;
}

[layout~="gap-x-small"] {
gap: var(--spacing__x-small);
}

[layout~="gap-small"] {
gap: var(--spacing__small);
}

[layout~="gap-medium"] {
gap: var(--spacing__medium);
}

[layout~="gap-large"] {
gap: var(--spacing__large);
}

[layout] > [layout] {
width: auto;
}
5 changes: 5 additions & 0 deletions app/src/assets/styles/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
--colour__alert: #DC2626;
--colour__alert--dark: #B91C1C;

--spacing__x-small: 0.5rem;
--spacing__small: 1rem;
--spacing__medium: 1.5rem;
--spacing__large: 2rem;

--border__colour: #EDEDED;

--animation__timing: 250ms;
Expand Down
9 changes: 1 addition & 8 deletions app/src/components/modals/add-clock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</meta-text>
</div>
<template #footer="{ close }">
<div class="add-clock-modal__actions" layout="row center-right">
<div class="add-clock-modal__actions" layout="row center-right gap-small">
<button class="button" @click="close()">Cancel</button>
<button class="button button--primary" :disabled="!canComplete" @click="complete()">Add {{ model.selection.length }} Clocks</button>
</div>
Expand Down Expand Up @@ -98,11 +98,4 @@ defineExpose({
margin: 1rem 0;
}

.add-clock-modal__actions {

& .button {
margin-left: 1rem;
}
}

</style>
4 changes: 4 additions & 0 deletions app/src/stores/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
export {
state,
computeState,
undo,
redo,
canUndo,
canRedo,
} from './store';

export * from './getters';
Expand Down
15 changes: 15 additions & 0 deletions app/src/stores/app/store.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import getState from './state';
import composeExtension from '@harlem/extension-compose';
import storageExtension from '@harlem/extension-storage';
import historyExtension from '@harlem/extension-history';

import {
ACTIONS,
MUTATIONS,
NAME,
} from './constants';
Expand All @@ -18,9 +20,22 @@ export const {
action,
reset,
computeState,
undo,
redo,
canUndo,
canRedo,
} = createStore(NAME, getState(), {
extensions: [
composeExtension(),
historyExtension({
mutations: {
include: '*',
exclude: [
MUTATIONS.updateTime,
ACTIONS.loadTimezones,
],
},
}),
storageExtension({
exclude: [
MUTATIONS.updateTime,
Expand Down
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
"vue": "^3.2.0"
},
"devDependencies": {
"vue": "^3.2.45"
"vue": "^3.2.47"
}
}
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
},
"devDependencies": {
"dotenv": "^16.0.3",
"vitepress": "^1.0.0-alpha.30"
"vitepress": "^1.0.0-alpha.47"
},
"dependencies": {
"@vueuse/core": "^9.6.0"
"@vueuse/core": "^9.13.0"
}
}
2 changes: 1 addition & 1 deletion examples/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"@nuxtjs/harlem": "^1.2.0"
},
"devDependencies": {
"nuxt": "^3.0.0"
"nuxt": "^3.2.2"
}
}
6 changes: 3 additions & 3 deletions examples/vite-ts-actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"@harlem/extension-compose": "^3.0.0",
"@harlem/extension-storage": "^3.0.0",
"@harlem/plugin-devtools": "^3.0.0",
"vue": "^3.2.45"
"vue": "^3.2.47"
},
"devDependencies": {
"@vitejs/plugin-vue": "^3.2.0",
"vite": "^3.2.5"
"@vitejs/plugin-vue": "^4.0.0",
"vite": "^4.1.4"
}
}
6 changes: 3 additions & 3 deletions examples/vite-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"@harlem/extension-compose": "^3.0.0",
"@harlem/extension-storage": "^3.0.0",
"@harlem/plugin-devtools": "^3.0.0",
"vue": "^3.2.45"
"vue": "^3.2.47"
},
"devDependencies": {
"@vitejs/plugin-vue": "^3.2.0",
"vite": "^3.2.5"
"@vitejs/plugin-vue": "^4.0.0",
"vite": "^4.1.4"
}
}
2 changes: 1 addition & 1 deletion extensions/action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"devDependencies": {
"@harlem/core": "^3.0.2",
"@harlem/testing": "^3.0.0",
"vue": "^3.2.45"
"vue": "^3.2.47"
}
}
2 changes: 1 addition & 1 deletion extensions/compose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
},
"devDependencies": {
"@harlem/core": "^3.0.2",
"vue": "^3.2.45"
"vue": "^3.2.47"
}
}
2 changes: 1 addition & 1 deletion extensions/compose/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function composeExtension<TState extends BaseState>() {
accessor(value);

const nodes = getNodes();
const name = mutationName || `extension:compose:${objectToPath(['root', ...nodes])}`;
const name = mutationName || `!extension:compose:${objectToPath(['root', ...nodes])}`;
const key = nodes.pop();
const parent = (state: object) => objectFromPath(state, nodes) as Record<PropertyKey, unknown>;

Expand Down
6 changes: 4 additions & 2 deletions extensions/history/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@
"@harlem/utilities": "^3.0.2"
},
"peerDependencies": {
"@harlem/core": "^3.0.0"
"@harlem/core": "^3.0.0",
"vue": "^3.2.0"
},
"devDependencies": {
"@harlem/core": "^3.0.2",
"@harlem/testing": "^3.0.0"
"@harlem/testing": "^3.0.0",
"vue": "^3.2.47"
}
}
19 changes: 17 additions & 2 deletions extensions/history/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@ import type {

export const SENDER = 'extension:history';
export const MUTATION_FILTER = /^(plugin|extension)/;
export const DEFAULT_GROUP_KEY = 'default';

export const TYPE_OFFSET: Record<ChangeType, number> = {
undo: -1,
redo: 1,
};

export const CHANGE_MAP: Record<ChangeType, Partial<ChangeCommands>> = {
exec: {
redo: {
set: (target, prop, newValue) => target[prop] = newValue,
deleteProperty: (target, prop) => delete target[prop],
},
undo: {
set: (target, prop, newValue, oldValue) => target[prop] = oldValue,
deleteProperty: (target, prop, newValue, oldValue) => target[prop] = oldValue,
},
};
};

export const EVENTS = {
change: {
before: 'history:change:before',
after: 'history:change:after',
success: 'history:change:success',
error: 'history:change:error',
},
} as const;
Loading