mirror of
https://github.com/heyman/heynote.git
synced 2025-06-27 04:51:50 +02:00
Sort notes in notes selector by how recent they were opened
This commit is contained in:
parent
5b61a0a234
commit
edcf33a606
@ -1,5 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { mapState, mapActions } from 'pinia'
|
import { mapState, mapActions } from 'pinia'
|
||||||
|
import { toRaw } from 'vue';
|
||||||
import { useNotesStore } from "../stores/notes-store"
|
import { useNotesStore } from "../stores/notes-store"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -23,15 +24,44 @@
|
|||||||
"scratch": path === "buffer-dev.txt",
|
"scratch": path === "buffer-dev.txt",
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
if (this.items.length > 1) {
|
||||||
|
this.selected = 1
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
computed: {
|
computed: {
|
||||||
...mapState(useNotesStore, [
|
...mapState(useNotesStore, [
|
||||||
"notes",
|
"notes",
|
||||||
|
"recentNotePaths",
|
||||||
]),
|
]),
|
||||||
|
|
||||||
|
orderedItems() {
|
||||||
|
const sortKeys = Object.fromEntries(this.recentNotePaths.map((item, idx) => [item, idx]))
|
||||||
|
const getSortScore = (item) => sortKeys[item.path] !== undefined ? sortKeys[item.path] : 1000
|
||||||
|
const compareFunc = (a, b) => {
|
||||||
|
const sortScore = getSortScore(a) - getSortScore(b)
|
||||||
|
if (sortScore !== 0) {
|
||||||
|
// sort by recency first
|
||||||
|
return sortScore
|
||||||
|
} else {
|
||||||
|
// then notes in root
|
||||||
|
const aIsRoot = a.path.indexOf("/") === -1
|
||||||
|
const bIsRoot = b.path.indexOf("/") === -1
|
||||||
|
if (aIsRoot && !bIsRoot) {
|
||||||
|
return -1
|
||||||
|
} else if (!aIsRoot && bIsRoot) {
|
||||||
|
return 1
|
||||||
|
} else {
|
||||||
|
// last sort by path
|
||||||
|
return a.path.localeCompare(b.path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [...this.items].sort(compareFunc)
|
||||||
|
},
|
||||||
|
|
||||||
filteredItems() {
|
filteredItems() {
|
||||||
return this.items.filter((lang) => {
|
return this.orderedItems.filter((lang) => {
|
||||||
return lang.name.toLowerCase().indexOf(this.filter.toLowerCase()) !== -1
|
return lang.name.toLowerCase().indexOf(this.filter.toLowerCase()) !== -1
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -2,11 +2,15 @@ import { toRaw } from 'vue';
|
|||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import { NoteFormat } from "../editor/note-format"
|
import { NoteFormat } from "../editor/note-format"
|
||||||
|
|
||||||
|
const SCRATCH_FILE = window.heynote.isDev ? "buffer-dev.txt" : "buffer.txt"
|
||||||
|
|
||||||
export const useNotesStore = defineStore("notes", {
|
export const useNotesStore = defineStore("notes", {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
notes: {},
|
notes: {},
|
||||||
|
recentNotePaths: [SCRATCH_FILE],
|
||||||
|
|
||||||
currentEditor: null,
|
currentEditor: null,
|
||||||
currentNotePath: window.heynote.isDev ? "buffer-dev.txt" : "buffer.txt",
|
currentNotePath: SCRATCH_FILE,
|
||||||
currentNoteName: null,
|
currentNoteName: null,
|
||||||
currentLanguage: null,
|
currentLanguage: null,
|
||||||
currentLanguageAuto: null,
|
currentLanguageAuto: null,
|
||||||
@ -32,6 +36,10 @@ export const useNotesStore = defineStore("notes", {
|
|||||||
this.showLanguageSelector = false
|
this.showLanguageSelector = false
|
||||||
this.showCreateNote = false
|
this.showCreateNote = false
|
||||||
this.currentNotePath = path
|
this.currentNotePath = path
|
||||||
|
|
||||||
|
const recent = this.recentNotePaths.filter((p) => p !== path)
|
||||||
|
recent.unshift(path)
|
||||||
|
this.recentNotePaths = recent.slice(0, 100)
|
||||||
},
|
},
|
||||||
|
|
||||||
openLanguageSelector() {
|
openLanguageSelector() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user