Skip to content

Commit

Permalink
make revert button do thing
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Jan 22, 2022
1 parent 2c71685 commit a29b740
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 29 deletions.
10 changes: 10 additions & 0 deletions src/lib/RevertButton.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts">
export let id: string
async function revert() {
// post request to /api/revert/<id>.json
await fetch(`/api/revert/${id}.json`, { method: 'POST' })
}
</script>

<button on:click={revert} class="revert-button">Revert</button>
14 changes: 13 additions & 1 deletion src/lib/database/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export async function fetchEntryHistoryItemsAfter(item: HistoryItem): Promise<Hi
/**
* Get the history item that was made right before this one, so we can calculate the diff
*/
export async function getEntryHistoryItemBefore(item: HistoryItem): Promise<HistoryItem | null> {
export async function fetchEntryHistoryItemBefore(item: HistoryItem): Promise<HistoryItem | null> {
const collection = await getCollection()

const result = await collection
Expand All @@ -107,3 +107,15 @@ export async function getEntryHistoryItemBefore(item: HistoryItem): Promise<Hist
if (result.length === 0) return null
return replaceUuidWithId(result[0])
}

/**
* Get the history item that was made right before this one, so we can calculate the diff
*/
export async function fetchEntryHistoryItem(id: string): Promise<HistoryItem | null> {
const collection = await getCollection()

const result = await collection.findOne(replaceIdWithUuid({ id }))

if (result === null) return null
return replaceUuidWithId(result)
}
33 changes: 6 additions & 27 deletions src/lib/revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Entry } from './database/entries'
import {
fetchEntryHistory,
fetchEntryHistoryItemsAfter,
getEntryHistoryItemBefore,
fetchEntryHistoryItemBefore,
HistoryItem,
} from './database/history'
import * as diff from 'fast-myers-diff'
Expand All @@ -13,9 +13,9 @@ import { createUuid } from './database'
*
* This code is very bad and buggy
*/
async function getRevertResult(historyItem: HistoryItem) {
export async function getRevertResult(historyItem: HistoryItem) {
const historyItemsAfter = await fetchEntryHistoryItemsAfter(historyItem)
const historyItemBefore = await getEntryHistoryItemBefore(historyItem)
const historyItemBefore = await fetchEntryHistoryItemBefore(historyItem)

if (historyItemBefore === null) {
// this was the initial edit, so we can't revert it
Expand All @@ -24,15 +24,13 @@ async function getRevertResult(historyItem: HistoryItem) {

// the difference between this edit and the previous one
let patch = Array.from(diff.calcPatch(historyItem.content, historyItemBefore.content))
console.log('patch', patch)
console.log('initial patch', patch)

// we update the initial patch based on all the edits that occured after this one
let tempBefore = historyItem
for (const historyItemAfter of historyItemsAfter) {
const diffAfter = Array.from(diff.diff(tempBefore.content, historyItemAfter.content))
console.log('tempDiff', diffAfter)
for (const [removeStart, removeEnd, insertStart, insertEnd] of diffAfter) {
const initialPatch = JSON.parse(JSON.stringify(patch))
patch = patch.map(([patchRemoveStart, patchRemoveEnd, insertSlice]) => {
// if the patch is removing something, subtract from the patchRemoveStart and patchRemoveEnd
if (removeStart !== removeEnd) {
Expand All @@ -57,12 +55,12 @@ async function getRevertResult(historyItem: HistoryItem) {

return [patchRemoveStart, patchRemoveEnd, insertSlice]
})
console.log(initialPatch, patch)
}
console.log(diffAfter)
tempBefore = historyItem
}

console.log('patch', patch)

// apply the patch to the current version
const newContent = Array.from(
diff.applyPatch(
Expand All @@ -73,24 +71,5 @@ async function getRevertResult(historyItem: HistoryItem) {
.flat()
.join('')

console.log(
'new',
Array.from(
diff.calcPatch(
historyItemsAfter[0] ? historyItemsAfter[0].content : historyItem.content,
newContent
)
)
)

return newContent
}

;(async () => {
console.log('...')
const history = await fetchEntryHistory(createUuid('a8942d398dd845b28822b2c19ae443e2'), 20, 0)
console.log('.....', history.items.length)
const newContent = await getRevertResult(history.items[1])

console.log(newContent)
})()
42 changes: 42 additions & 0 deletions src/routes/api/revert/[id].json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { RequestHandler } from '@sveltejs/kit'
import type { JSONString } from '@sveltejs/kit/types/helper'
import { fetchEntryHistoryItem } from '../../../lib/database/history'
import { getRevertResult } from '../../../lib/revert'

export interface APIHistoryItem {
id: string
entryId: string
userId: string
title: string
content: string
timestamp: Date
}

export interface APIHistoryResponse {
count: number
items: APIHistoryItem[]
}

// revert an entry history item
export const post: RequestHandler = async req => {
const historyItemId = req.params.id

const historyItem = await fetchEntryHistoryItem(historyItemId)

if (historyItem === null)
return {
status: 404,
body: {
error: 'Not found',
},
}

const newContent = await getRevertResult(historyItem)

console.log(newContent)
return {
body: {
content: newContent,
} as unknown as JSONString,
}
}
3 changes: 2 additions & 1 deletion src/routes/history/[id].svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import User from '../../lib/User.svelte'
import { format as formatTimeAgo } from 'timeago.js'
import { browser } from '$app/env'
import RevertButton from '../../lib/RevertButton.svelte'
export let entry: Entry
export let historyItems: APIHistoryItem[]
Expand Down Expand Up @@ -74,7 +75,7 @@
{#if i > 0}
<div class="history-item">
<User id={historyItem.userId} /> - {formatTimeAgo(historyItem.timestamp)}
<button class="revert-button">Revert</button>
<RevertButton id={historyItems[i - 1].id} />

<div class="history-diff">
<Diff
Expand Down

0 comments on commit a29b740

Please sign in to comment.