Skip to content

Commit 40dee8e

Browse files
committed
merge locate logic of cursor and scroll
1 parent 9310fd3 commit 40dee8e

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

main.css

+35-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Outline.vue

+13-13
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<script setup lang="ts">
3434
import { ref, toRef, reactive, toRaw, computed, watch, nextTick, getCurrentInstance, onMounted, onUnmounted, HTMLAttributes, h, watchEffect } from 'vue';
35-
import { Notice, MarkdownView, sanitizeHTMLToDom, HeadingCache } from 'obsidian';
35+
import { Notice, MarkdownView, sanitizeHTMLToDom, HeadingCache, debounce } from 'obsidian';
3636
import { NTree, TreeOption, NButton, NInput, NSlider, NConfigProvider, darkTheme, GlobalThemeOverrides, TreeDropInfo } from 'naive-ui';
3737
import { Icon } from '@vicons/utils';
3838
import { SettingsBackupRestoreRound, ArrowCircleDownRound } from '@vicons/material';
@@ -199,16 +199,16 @@ function _handleScroll(evt: Event) {
199199
return;
200200
}
201201
202-
if (plugin.settings.locate_by_cursor) {
203-
return;
204-
}
202+
// if (plugin.settings.locate_by_cursor) {
203+
// return;
204+
// }
205205
206-
onPosChange();
206+
onPosChange(true);
207207
}
208208
209-
function onPosChange() {
210-
let current = currentLine();
211-
let index = nearestHeading(current);
209+
function onPosChange(fromScroll: boolean) {
210+
const current = currentLine(fromScroll);
211+
const index = nearestHeading(current);
212212
if (index === undefined) return;
213213
214214
autoExpand(index);
@@ -226,16 +226,16 @@ onUnmounted(() => {
226226
function handleCursorChange() {
227227
228228
if (plugin.settings.locate_by_cursor) {
229-
onPosChange();
229+
onPosChange(false);
230230
}
231231
}
232232
233-
function currentLine() {
233+
function currentLine(fromScroll: boolean) {
234234
// const view = plugin.app.workspace.getActiveViewOfType(MarkdownView);
235235
const view = plugin.current_note;
236236
if (!view || view.getViewType() !== "markdown") return;
237237
238-
if (plugin.settings.locate_by_cursor) {
238+
if (plugin.settings.locate_by_cursor && !fromScroll) {
239239
return (view as MarkdownView).editor.getCursor("from").line;
240240
} else {
241241
// @ts-ignore
@@ -367,11 +367,11 @@ function _openPopover(e: KeyboardEvent) {
367367
}
368368
}
369369
370-
const openPopover = debounce(_openPopover, 100);
370+
const openPopover = customDebounce(_openPopover, 100);
371371
372372
// excute on first time, and wait until next fresh
373373
// ... or take a break when node pointed changes
374-
function debounce(func: (_: any) => void, delay: number) {
374+
function customDebounce(func: (_: any) => void, delay: number) {
375375
let fresh = true;
376376
let timeoutId: any;
377377

0 commit comments

Comments
 (0)