aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/(editor)/lib/debouncedsave.ts
blob: 6490c6c4990a4d079b95fd6cb2d69b3620721f5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import hljs from 'highlight.js'
import { debounce } from 'tldraw';
import { useDebouncedCallback } from "use-debounce";

export const Updates = debounce(({editor, setCharsCount, setSaveStatus})=> {
  const json = editor.getJSON();
  setCharsCount(editor.storage.characterCount.words());
  window.localStorage.setItem("html-content", highlightCodeblocks(editor.getHTML()));
  window.localStorage.setItem("novel-content", JSON.stringify(json));
  window.localStorage.setItem("markdown", editor.storage.markdown.getMarkdown());
  setSaveStatus("Saved");
}, 500)

export const highlightCodeblocks = (content: string) => {
  const doc = new DOMParser().parseFromString(content, 'text/html');
  doc.querySelectorAll('pre code').forEach((el) => {
    hljs.highlightElement(el);
  });
  return new XMLSerializer().serializeToString(doc);
};