Skip to content

Commit

Permalink
Merge pull request #67 from sidharthv96/relative_time
Browse files Browse the repository at this point in the history
Relative Time in History
  • Loading branch information
knsv authored Dec 17, 2020
2 parents b3827e5 + 116cb7c commit c34800e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/routes/Edit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { fromUrl } from "../code-store.js";
// import pkg from '@mermaid-js/mermaid/package.json'
import pkg from "@mermaid/package.json";
import moment from "moment";
export let mermaidVersion = pkg.version;
let historyList = [];
onMount(async () => {
Expand Down Expand Up @@ -38,18 +39,19 @@
setInterval(() => {
if (code != hisCode) {
//save history
historyList.push({
historyList.unshift({
time: new Date().toISOString(),
code: hisCode = code
});
if (historyList.length > 10) {
historyList.shift();
historyList.pop();
}
historyList = historyList; //triggered update
localStorage.setItem(historyListKey, JSON.stringify(historyList));
}
historyList = historyList; //triggered update
}, 1 * 60 * 1000);
});
// export let code = '';
// export let classes = '';
// export let error = {};
Expand Down Expand Up @@ -170,6 +172,10 @@
if (!code) return;
updateCode(code, true);
}
function relativeTime(t){
return `${moment(t).fromNow()} (${new Date(t).toLocaleString()})`;
}
</script>

<style>
Expand Down Expand Up @@ -231,6 +237,8 @@
.button-container {
display: flex;
align-items: center;
flex-direction: row;
flex-wrap: wrap;
}
.button-style {
background-color: #a2d9e2;
Expand Down Expand Up @@ -313,7 +321,7 @@
{#if historyList.length > 0}
{#each historyList as item, i}
<button class="button-style" on:click="{e => toUpdateCodeStore(item.code)}">
-{historyList.length - i} min
{relativeTime(item.time)}
</button>
{/each}
{:else}
Expand Down

0 comments on commit c34800e

Please sign in to comment.