Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useTimeline): add
useTimeline
composable (#342)
* feat(useTimeline): add `useTimeline` composable * chore: set timeline list to readonly * chore: add todo tests * chore: fix build * chore: fix readonly when ref * chore: add docs * chore: fix tests * chore: add docs title
- Loading branch information
Showing
17 changed files
with
297 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<template> | ||
<div> | ||
<p>Type a text to enable undo and redo</p> | ||
<input v-model="value" /> | ||
|
||
<p> | ||
<b>History</b> | ||
{{ timeline }} | ||
</p> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { ref } from "@vue/composition-api"; | ||
import { useTimeline } from "vue-composable"; | ||
export default { | ||
setup() { | ||
const value = ref(""); | ||
const timeline = useTimeline(value); | ||
return { | ||
value, | ||
timeline | ||
}; | ||
} | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# useTimeline | ||
|
||
> Tracks variable history | ||
## Parameters | ||
|
||
```js | ||
import { useTimeline } from "vue-composable"; | ||
|
||
const options = { | ||
deep: Boolean, | ||
maxLength: Number, | ||
clone(entry: T): T | ||
} | ||
|
||
const timeline = useTimeline(value, options); | ||
``` | ||
|
||
| Parameters | Type | Required | Default | Description | | ||
| ---------- | -------------------- | -------- | ----------------------------------------------------------- | ---------------------- | | ||
| value | `Ref<T>` | `true` | | `ref` to track history | | ||
| options | `TimelineOptions<T>` | `false` | `{ deep: false, maxLength: MAX_ARRAY_SIZE, clone: (x)=>x }` | timeline options | | ||
|
||
::: tip | ||
|
||
If tracking object please provide a `options.clone` function. | ||
|
||
```ts | ||
// example | ||
function clone(e) { | ||
return JSON.parse(JSON.stringify(e)); | ||
} | ||
``` | ||
|
||
::: | ||
|
||
## State | ||
|
||
The `useTimeline` function exposes the following reactive state: | ||
|
||
```js | ||
import { useTimeline } from "vue-composable"; | ||
|
||
const timeline = useTimeline(); | ||
``` | ||
|
||
| State | Type | Description | | ||
| -------- | ------------------------------ | ---------------- | | ||
| timeline | `Ref<{item: T, date: Date}[]>` | `timeline` array | | ||
|
||
## Example | ||
|
||
<timeline-example/> | ||
|
||
### Code | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<p>Type a text to enable undo and redo</p> | ||
<input v-model="value" /> | ||
<p> | ||
<b>History</b> | ||
{{ timeline }} | ||
</p> | ||
</div> | ||
</template> | ||
<script> | ||
import { ref } from "@vue/composition-api"; | ||
import { useTimeline } from "vue-composable"; | ||
export default { | ||
setup() { | ||
const value = ref(""); | ||
const timeline = useTimeline(value); | ||
return { | ||
value, | ||
timeline | ||
}; | ||
} | ||
}; | ||
</script> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useTimeline } from "../../src"; | ||
import { ref } from "../../src/api"; | ||
|
||
describe("timeline", () => { | ||
it("should work", () => { | ||
const value = ref(""); | ||
const timeline = useTimeline(value); | ||
|
||
expect(timeline.value).toMatchObject([]); | ||
|
||
value.value = "1"; | ||
expect(timeline.value).toMatchObject([{ item: "" }]); | ||
|
||
value.value = "2"; | ||
expect(timeline.value).toMatchObject([{ item: "1" }, { item: "" }]); | ||
}); | ||
|
||
it("should not store more than the maxLength", () => { | ||
const value = ref(""); | ||
const timeline = useTimeline(value, { maxLength: 1 }); | ||
|
||
expect(timeline.value).toMatchObject([]); | ||
|
||
value.value = "1"; | ||
expect(timeline.value).toMatchObject([{ item: "" }]); | ||
|
||
value.value = "2"; | ||
expect(timeline.value).toMatchObject([{ item: "1" }]); | ||
}); | ||
|
||
it("should use the clone option", () => { | ||
const value = ref(""); | ||
const clone = jest.fn().mockImplementation(x => `x${x}`); | ||
const timeline = useTimeline(value, { clone }); | ||
|
||
expect(timeline.value).toMatchObject([]); | ||
|
||
value.value = "1"; | ||
expect(clone).toHaveBeenCalledTimes(1); | ||
expect(timeline.value).toMatchObject([{ item: "x" }]); | ||
|
||
value.value = "2"; | ||
expect(clone).toHaveBeenCalledTimes(2); | ||
expect(timeline.value).toMatchObject([{ item: "x1" }, { item: "x" }]); | ||
}); | ||
|
||
it("should watch deep changes", () => { | ||
const value = ref({ a: 1 }); | ||
const timeline = useTimeline(value, { deep: true }); | ||
|
||
expect(timeline.value).toMatchObject([]); | ||
|
||
value.value.a++; | ||
expect(timeline.value).toMatchObject([{ item: value.value }]); | ||
|
||
value.value.a++; | ||
expect(timeline.value).toMatchObject([ | ||
{ item: value.value }, | ||
{ item: value.value } | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.