Sha256: 4394a022a59900f75b0965aed1b6454d6e09aa9b0f4457b963192b74b27ef16c

Contents?: true

Size: 1015 Bytes

Versions: 1

Compression:

Stored size: 1015 Bytes

Contents

import Fuse from 'https://cdn.jsdelivr.net/npm/fuse.js@6.6.2/dist/fuse.esm.js';

export function createSearch() {
  return {
    query: '',
    results: [],
    fuse: null,
    lastResults: [],

    async init() {
      try {
        const res = await fetch("/search.json");
        if (!res.ok) {
          throw new Error(`Error HTTP status: ${res.status}`);
        }
        const data = await res.json();
        this.fuse = new Fuse(data, { keys: ["title", "summary"], threshold: 0.3 });
      } catch (error) {
        console.error("Error fetching data:", error);
      }
    },

    search() {
      if (!this.fuse || this.query.trim() === '') {
        this.clearResults();
        return;
      }
      this.results = this.fuse.search(this.query);
      this.lastResults = this.results;
    },

    clearResults() {
      this.results = [];
    },

    restoreResults() {
      if (this.query.trim() !== '' && this.lastResults.length > 0) {
        this.results = this.lastResults;
      }
    },
  };
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cosensee-0.8.0 assets/js/search.js