From 69fea8aa39645e1565b771b78c4bdd61860a9f33 Mon Sep 17 00:00:00 2001 From: anian Date: Sat, 18 Jul 2026 15:34:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AF=B9PJAX=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 ++ build.gradle | 2 +- gradle.properties | 2 +- packages/search-widget/src/search-form.ts | 131 ++++++++++++++------- packages/search-widget/src/search-modal.ts | 1 + 5 files changed, 101 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index d253fe3..775d37a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ +# anian-plugin-search + +> 基于原作者v1.7.1代码修改 + +#### v1.7.1-1 +1. 新增对PJAX的支持 + + + # plugin-search-widget Halo 2.0 的通用搜索组件插件。 diff --git a/build.gradle b/build.gradle index de469d8..6091bf7 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ repositories { } dependencies { - implementation platform('run.halo.tools.platform:plugin:2.17.0-SNAPSHOT') + implementation platform('run.halo.tools.platform:plugin:2.20.11') compileOnly 'run.halo.app:api' testImplementation 'run.halo.app:api' diff --git a/gradle.properties b/gradle.properties index 8d0c7be..b3329f9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=1.0.0-SNAPSHOT +version=1.7.1-1 diff --git a/packages/search-widget/src/search-form.ts b/packages/search-widget/src/search-form.ts index 695ec62..5f0e65a 100644 --- a/packages/search-widget/src/search-form.ts +++ b/packages/search-widget/src/search-form.ts @@ -181,51 +181,55 @@ export class SearchForm extends LitElement { renderListItem(hit: HaloDocument, index: number, listIcon: string) { return html`
  • { this.selectedIndex = index; }} - class="shadow-sm flex items-center space-x-3 rounded-base cursor-pointer p-3 bg-hit [&_mark]:text-primary [&_mark]:font-semibold [&_mark]:bg-transparent ${ - index === this.selectedIndex - ? '!bg-primary [&_mark]:!text-white [&_mark]:underline' - : '' - }" data-index=${index} > - -
    -

    - ${unsafeHTML(hit.title)} -

    - ${ - hit.description - ? html` -

    - ${unsafeHTML(hit.description)} -

    - ` + + > + +
    +

    + ${unsafeHTML(hit.title)} +

    + ${ + hit.description + ? html` +

    + ${unsafeHTML(hit.description)} +

    + ` + : '' + } +
    + +
  • `; } @@ -247,7 +251,7 @@ export class SearchForm extends LitElement { this.fetchHits(value); } - handleClearHistory() { + private handleClearHistory() { localStorage.removeItem(HISTORY_KEY); this.historyHits = []; } @@ -280,14 +284,56 @@ export class SearchForm extends LitElement { 300 ); - handleOpenLink(hit: HaloDocument) { + handleOpenLink(hit: HaloDocument, event?: MouseEvent) { const updatedHistory = uniqBy([hit, ...this.historyHits], 'id').slice( 0, MAX_HISTORY_ITEMS ); localStorage.setItem(HISTORY_KEY, JSON.stringify(updatedHistory)); this.historyHits = updatedHistory; - window.location.href = hit.permalink; + + if (event && this.shouldUseNativeNavigation(event)) { + return; + } + + event?.preventDefault(); + + this.navigate(hit.permalink); + + queueMicrotask(() => { + this.dispatchEvent( + new CustomEvent('search-widget:navigate', { + bubbles: true, + composed: true, + }) + ); + }); + } + + private shouldUseNativeNavigation(event: MouseEvent) { + return ( + event.defaultPrevented || + event.button !== 0 || + event.metaKey || + event.ctrlKey || + event.shiftKey || + event.altKey + ); + } + + private navigate(url: string) { + const win = window as Window & { + pjax?: { + loadUrl(url: string): void; + }; + }; + + if (win.pjax) { + win.pjax.loadUrl(url); + return; + } + + window.location.href = url; } handleKeydown = (e: KeyboardEvent) => { @@ -315,11 +361,12 @@ export class SearchForm extends LitElement { const hit = hits[this.selectedIndex]; if (hit) { this.handleOpenLink(hit); + e.preventDefault(); } } }; - handleScrollIntoSelected() { + private handleScrollIntoSelected() { const selectedElement = this.shadowRoot?.querySelector( `[data-index="${this.selectedIndex}"]` ); diff --git a/packages/search-widget/src/search-modal.ts b/packages/search-widget/src/search-modal.ts index f716d8e..4ac699a 100644 --- a/packages/search-widget/src/search-modal.ts +++ b/packages/search-widget/src/search-modal.ts @@ -64,6 +64,7 @@ export class SearchModal extends LitElement { ? html`` : '' }