Skip to content
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
7 changes: 7 additions & 0 deletions plugins/ztools-markdown-notes/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# 更新日志

## 0.1.4 - 2026-10-28

- 新增目录树自定义排序,支持在同一目录内拖拽混排文件夹和笔记。
- 优化拖拽反馈,通过条目边缘插入线调整顺序,拖入文件夹中部执行移动。
- 排序按工作区和目录独立保存,并在重命名、移动、刷新后保持一致。
- 排序记录读取或保存失败时显示明确提示,且不影响已经完成的文件操作。

## 0.1.3 - 2026-10-12

- 新增黑色、白色等内置主题,并支持跟随系统切换蓝色主题的明暗配色。
Expand Down
2 changes: 1 addition & 1 deletion plugins/ztools-markdown-notes/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ztools-markdown-notes",
"private": true,
"version": "0.1.3",
"version": "0.1.4",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion plugins/ztools-markdown-notes/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "Markdown 笔记",
"description": "基于真实本地 Markdown 文件的轻量笔记插件",
"author": "dufan1032",
"version": "0.1.3",
"version": "0.1.4",
"main": "index.html",
"development": {
"main": "http://localhost:5173"
Expand Down
2 changes: 1 addition & 1 deletion plugins/ztools-markdown-notes/public/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "Markdown 笔记",
"description": "基于真实本地 Markdown 文件的轻量笔记插件",
"author": "dufan1032",
"version": "0.1.3",
"version": "0.1.4",
"main": "index.html",
"development": {
"main": "http://localhost:5173"
Expand Down
50 changes: 49 additions & 1 deletion plugins/ztools-markdown-notes/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import WorkspaceTree from './components/WorkspaceTree.vue'
import { extractOutline } from './outline'
import { isDarkColor, resolveTheme, type ColorTheme } from './themes'
import { endWorkspaceDrag, getWorkspaceDragPath } from './workspaceDrag'
import { reorderWorkspaceEntry, replaceWorkspaceOrderPath, sortWorkspaceEntries } from './workspaceOrder'
import { clearNoteDraft, clearNoteDraftPath, defaultEditorSettings, loadEditorMode, loadEditorSettings, loadGlobalNoteSearchEnabled, loadGlobalWorkspaceSearchEnabled, loadHiddenGlobalNotePaths, loadLastWorkspacePath, loadNoteDraft, loadPinnedNotePaths, loadRegisteredWorkspaces, loadTrashRetentionDays, replaceNoteDraftPath, saveEditorMode, saveEditorSettings, saveGlobalNoteSearchEnabled, saveGlobalWorkspaceSearchEnabled, saveHiddenGlobalNotePaths, saveLastWorkspacePath, saveNoteDraft, savePinnedNotePaths, saveRegisteredWorkspaces, saveTrashRetentionDays, type EditorSettings, type NoteDraft, type RegisteredWorkspace } from './workspaceSession'

const workspace = ref<WorkspaceScan | null>(null)
const orderedTreeEntries = ref<WorkspaceTreeEntry[]>([])
const treeOrderError = ref('')
const previousWorkspace = ref<WorkspaceScan | null>(null)
const workspaceName = ref('我的笔记')
const selectedPath = ref('')
Expand Down Expand Up @@ -374,6 +377,40 @@ function toggleGlobalWorkspaceSearchEnabled() {
void syncGlobalNoteFeatures()
}

function reloadTreeOrder() {
const current = workspace.value
if (!current) {
orderedTreeEntries.value = []
return
}
try {
orderedTreeEntries.value = sortWorkspaceEntries(current.workspace.id, current.entries)
} catch (error) {
orderedTreeEntries.value = current.entries
treeOrderError.value = `读取排序失败,已使用默认顺序:${error instanceof Error ? error.message : '未知错误'}`
}
}

function reorderEntry(sourcePath: string, targetPath: string, position: 'before' | 'after') {
if (!workspace.value) return
try {
if (reorderWorkspaceEntry(workspace.value.workspace.id, workspace.value.entries, sourcePath, targetPath, position)) {
reloadTreeOrder()
}
} catch (error) {
treeOrderError.value = `保存排序失败:${error instanceof Error ? error.message : '未知错误'}`
}
}

function replaceTreeOrderPath(oldPath: string, newPath: string) {
if (!workspace.value) return
try {
replaceWorkspaceOrderPath(workspace.value.workspace.id, workspace.value.entries, oldPath, newPath)
} catch (error) {
treeOrderError.value = `文件操作已完成,但更新排序失败:${error instanceof Error ? error.message : '未知错误'}`
}
}

function replacePinnedPath(oldPath: string, newPath: string) {
const oldPrefix = `${oldPath}/`
const nextPaths = pinnedNotePaths.value.map((path) => {
Expand Down Expand Up @@ -424,6 +461,7 @@ async function moveEntry(sourcePath: string, targetDirectoryPath: string) {
}
const root = workspace.value.workspace.path
const newPath = await window.znotes.moveEntry(root, sourcePath, targetDirectoryPath)
replaceTreeOrderPath(sourcePath, newPath)
replacePinnedPath(sourcePath, newPath)
replaceHiddenGlobalPath(sourcePath, newPath)
const movedOpenNotePath = activeNotePath.value === sourcePath
Expand Down Expand Up @@ -1197,6 +1235,7 @@ async function submitRename() {
try {
const root = workspace.value.workspace.path
const newPath = await window.znotes.renameEntry(root, oldPath, renameName.value)
replaceTreeOrderPath(oldPath, newPath)
replacePinnedPath(oldPath, newPath)
replaceHiddenGlobalPath(oldPath, newPath)
replaceNoteDraftPath(workspace.value.workspace.id, oldPath, newPath)
Expand Down Expand Up @@ -1920,6 +1959,10 @@ function scheduleAutoSave() {

watch(editorContent, scheduleAutoSave)
watch(searchQuery, scheduleSearch)
watch(workspace, (current, previous) => {
if (current?.workspace.id !== previous?.workspace.id) treeOrderError.value = ''
reloadTreeOrder()
})
watch(workspace, (currentWorkspace) => {
if (!currentWorkspace) {
pinnedNotePaths.value = []
Expand Down Expand Up @@ -2061,9 +2104,13 @@ watch(activeNotePath, () => {
<button type="button" class="pinned-note-remove" aria-label="取消置顶" title="取消置顶" @click="togglePinnedNote(note)">×</button>
</div>
</section>
<div v-if="treeOrderError" class="tree-order-error" role="alert">
<span>{{ treeOrderError }}</span>
<button type="button" aria-label="关闭排序错误提示" @click="treeOrderError = ''">×</button>
</div>
<WorkspaceTree
v-if="workspace.entries.length"
:entries="workspace.entries"
:entries="orderedTreeEntries"
:active-path="activeNotePath"
:selected-path="selectedEntry?.relativePath ?? ''"
:create-parent-path="selectedDirectory"
Expand Down Expand Up @@ -2092,6 +2139,7 @@ watch(activeNotePath, () => {
@cancel-rename="cancelRename"
@toggle-menu="openEntryMenuPath = $event"
@request-move="moveEntry"
@request-reorder="reorderEntry"
@show-in-folder="showEntryInFolder"
/>
<p v-else class="muted">工作区中还没有 Markdown 笔记。</p>
Expand Down
62 changes: 47 additions & 15 deletions plugins/ztools-markdown-notes/src/components/WorkspaceTree.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script setup lang="ts">
import { ref } from 'vue'
import { ref, watch } from 'vue'
import InlineCreate from './InlineCreate.vue'
import { beginWorkspaceDrag, endWorkspaceDrag, getWorkspaceDragPath } from '../workspaceDrag'
import { beginWorkspaceDrag, endWorkspaceDrag, getWorkspaceDragPath, getTreeDropPosition, workspaceDragPath } from '../workspaceDrag'
import { parentDirectory } from '../workspaceOrder'

defineProps<{
entries: WorkspaceTreeEntry[]
Expand All @@ -20,7 +21,8 @@ defineProps<{
globalNoteSearchEnabled: boolean
}>()

const dropTarget = ref<{ path: string; allowed: boolean } | null>(null)
const dropTarget = ref<{ path: string; allowed: boolean; position: 'before' | 'after' | 'inside' } | null>(null)
watch(workspaceDragPath, () => { dropTarget.value = null }, { flush: 'sync' })

function canDropInto(sourcePath: string, targetPath: string) {
const parentPath = sourcePath.includes('/') ? sourcePath.slice(0, sourcePath.lastIndexOf('/')) : ''
Expand All @@ -43,6 +45,7 @@ const emit = defineEmits<{
cancelRename: []
toggleMenu: [relativePath: string | null]
requestMove: [sourcePath: string, targetDirectoryPath: string]
requestReorder: [sourcePath: string, targetPath: string, position: 'before' | 'after']
showInFolder: [entry: WorkspaceTreeEntry]
}>()

Expand All @@ -52,18 +55,42 @@ function startDrag(event: DragEvent, path: string) {
if (event.dataTransfer) event.dataTransfer.effectAllowed = 'move'
}

function dropInto(event: DragEvent, targetDirectoryPath: string) {
function resolveDrop(event: DragEvent, entry: WorkspaceTreeEntry) {
const row = event.currentTarget as HTMLElement
const rect = row.getBoundingClientRect()
const position = getTreeDropPosition(entry.kind, (event.clientY - rect.top) / Math.max(1, rect.height))
const sourcePath = getWorkspaceDragPath()
const allowed = sourcePath ? canDropInto(sourcePath, targetDirectoryPath) : false
dropTarget.value = null
if (sourcePath && allowed) emit('requestMove', sourcePath, targetDirectoryPath)
const allowed = position === 'inside'
? canDropInto(sourcePath, entry.relativePath)
: Boolean(sourcePath) && sourcePath !== entry.relativePath && parentDirectory(sourcePath) === parentDirectory(entry.relativePath)
return { path: entry.relativePath, allowed, position }
}

function showDropTarget(event: DragEvent, targetDirectoryPath: string) {
function dropOnEntry(event: DragEvent, entry: WorkspaceTreeEntry) {
const sourcePath = getWorkspaceDragPath()
dropTarget.value = { path: targetDirectoryPath, allowed: canDropInto(sourcePath, targetDirectoryPath) }
const target = resolveDrop(event, entry)
dropTarget.value = null
endWorkspaceDrag()
if (!target.allowed) return
if (target.position === 'inside') emit('requestMove', sourcePath, target.path)
else emit('requestReorder', sourcePath, target.path, target.position)
}

function showDropTarget(event: DragEvent, entry: WorkspaceTreeEntry) {
dropTarget.value = resolveDrop(event, entry)
if (event.dataTransfer) event.dataTransfer.dropEffect = dropTarget.value.allowed ? 'move' : 'none'
}

function dropClasses(entry: WorkspaceTreeEntry) {
const target = dropTarget.value
if (target?.path !== entry.relativePath) return {}
return {
'tree-row--drop-target': target.allowed && target.position === 'inside',
'tree-row--drop-invalid': !target.allowed,
'tree-row--insert-before': target.allowed && target.position === 'before',
'tree-row--insert-after': target.allowed && target.position === 'after',
}
}
</script>

<template>
Expand All @@ -83,20 +110,20 @@ function showDropTarget(event: DragEvent, targetDirectoryPath: string) {
<summary
draggable="true"
class="tree-row"
:class="{ 'tree-row--selected': entry.relativePath === selectedPath, 'tree-row--drop-target': dropTarget?.path === entry.relativePath && dropTarget.allowed, 'tree-row--drop-invalid': dropTarget?.path === entry.relativePath && !dropTarget.allowed }"
:class="[{ 'tree-row--selected': entry.relativePath === selectedPath }, dropClasses(entry)]"
@click.stop="emit('selectEntry', entry)"
@dragstart.stop="startDrag($event, entry.relativePath)"
@dragend="endWorkspaceDrag(); dropTarget = null"
@dragenter.prevent.stop="showDropTarget($event, entry.relativePath)"
@dragover.prevent.stop="showDropTarget($event, entry.relativePath)"
@dragenter.prevent.stop="showDropTarget($event, entry)"
@dragover.prevent.stop="showDropTarget($event, entry)"
@dragleave.stop="dropTarget = null"
@drop.prevent.stop="dropInto($event, entry.relativePath)"
@drop.prevent.stop="dropOnEntry($event, entry)"
>
<span class="tree-label">
<svg class="folder-chevron" viewBox="0 0 16 16" aria-hidden="true"><path d="m6 3.5 4 4.5-4 4.5" /></svg>
<svg class="entry-icon folder-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M3 6.5A1.5 1.5 0 0 1 4.5 5h5l2 2h8A1.5 1.5 0 0 1 21 8.5v9A1.5 1.5 0 0 1 19.5 19h-15A1.5 1.5 0 0 1 3 17.5z" /></svg>
{{ entry.name }}
<small v-if="dropTarget?.path === entry.relativePath" class="tree-drop-hint">{{ dropTarget.allowed ? '移动到这里' : '无法移动' }}</small>
<small v-if="dropTarget?.path === entry.relativePath" class="tree-drop-hint">{{ !dropTarget.allowed ? '无法放置' : dropTarget.position === 'inside' ? '移动到这里' : '调整顺序' }}</small>
</span>
<div class="entry-menu" :class="{ 'entry-menu--open': openMenuPath === entry.relativePath }" @click.stop>
<button type="button" class="entry-menu-trigger" aria-label="更多操作" @click="emit('toggleMenu', openMenuPath === entry.relativePath ? null : entry.relativePath)">
Expand Down Expand Up @@ -129,6 +156,7 @@ function showDropTarget(event: DragEvent, targetDirectoryPath: string) {
@cancel-rename="emit('cancelRename')"
@toggle-menu="emit('toggleMenu', $event)"
@request-move="(sourcePath, targetPath) => emit('requestMove', sourcePath, targetPath)"
@request-reorder="(sourcePath, targetPath, position) => emit('requestReorder', sourcePath, targetPath, position)"
@show-in-folder="emit('showInFolder', $event)"
/>
<InlineCreate
Expand All @@ -145,10 +173,14 @@ function showDropTarget(event: DragEvent, targetDirectoryPath: string) {
<div
v-else
class="tree-row"
:class="{ 'tree-row--active': entry.relativePath === activePath, 'tree-row--selected': entry.relativePath === selectedPath }"
:class="[{ 'tree-row--active': entry.relativePath === activePath, 'tree-row--selected': entry.relativePath === selectedPath }, dropClasses(entry)]"
draggable="true"
@dragstart.stop="startDrag($event, entry.relativePath)"
@dragend="endWorkspaceDrag(); dropTarget = null"
@dragenter.prevent.stop="showDropTarget($event, entry)"
@dragover.prevent.stop="showDropTarget($event, entry)"
@dragleave.stop="dropTarget = null"
@drop.prevent.stop="dropOnEntry($event, entry)"
>
<button type="button" class="tree-note" :title="entry.relativePath" @click="emit('selectEntry', entry); emit('openNote', entry.relativePath)">
<svg class="entry-icon note-icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3.5h8l4 4v13H6z"/><path d="M14 3.5v4h4M9 12h6M9 16h6"/></svg>
Expand Down
4 changes: 4 additions & 0 deletions plugins/ztools-markdown-notes/src/styles.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions plugins/ztools-markdown-notes/src/workspaceDrag.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
let draggedEntryPath = ''
import { ref } from 'vue'

export const workspaceDragPath = ref('')

export function beginWorkspaceDrag(path: string) {
draggedEntryPath = path
workspaceDragPath.value = path
}

export function getWorkspaceDragPath() {
return draggedEntryPath
return workspaceDragPath.value
}

export function endWorkspaceDrag() {
draggedEntryPath = ''
workspaceDragPath.value = ''
}

export function getTreeDropPosition(kind: 'note' | 'directory', ratio: number): 'before' | 'after' | 'inside' {
if (kind === 'directory' && ratio >= 0.25 && ratio <= 0.75) return 'inside'
return ratio < 0.5 ? 'before' : 'after'
}
Loading
Loading