obsidian-latex-suite icon indicating copy to clipboard operation
obsidian-latex-suite copied to clipboard

[FR] Saving Snippets in a Hidden Folder

Open zcysxy opened this issue 1 year ago • 5 comments

It would be great if the plugin can read a snippets file in a hidden directory like .config/

zcysxy avatar Oct 16 '22 21:10 zcysxy

I think it would be better to store snippets in the vault so I can edit the snippets at any time. but currently if you save the snippets as md files it can lead to confusing formatting.

I can add a feature to latex-suite that can open the snippets, I save it as a js file and it has syntax highlighting. Something like this.

image

The code is not much, if @artisticat1 is willing to add it would be better.

//register a textFileView

export class SampleView extends TextFileView {
  codeMirror: CodeMirror.Editor
  constructor(leaf: WorkspaceLeaf, public plugin: SamplePlugin) {
    super(leaf)
    // @ts-ignore
    this.codeMirror = CodeMirror(this.contentEl, {
      theme: "javascript",
      mode:  "javascript",
      addModeClass: true,
      autofocus: true
    })

    this.codeMirror.on("changes", () => this.codeMirror.refresh());
  }

  onResize() {
    this.codeMirror.refresh();
  }

  getViewData(): string {
    return this.codeMirror.getValue()
  }

  setViewData(data: string, clear: boolean): void {
    if (clear) {
        //@ts-ignore
        this.codeMirror.swapDoc(CodeMirror.Doc(data, "javascript"));
      } else {
          this.codeMirror.setValue(data);
      }

      // @ts-ignore
      if (this.app?.vault?.config?.vimMode) {
          this.codeMirror.setOption("keyMap", "vim");
      }

      // This seems to fix some odd visual bugs:
      this.codeMirror.refresh();

      // This focuses the editor, which is analogous to the
      // default Markdown behavior in Obsidian:
      this.codeMirror.focus();
  }

  clear(): void {
    this.codeMirror.setValue("");
    this.codeMirror.clearHistory();
  }

  getViewType() {
    return SAMPLE_VIEW_TYPE
  }

  getDisplayText() {
    if (this.file) {
        return this.file.basename;
    } else {
        return "js (No File)";
    }
  }

  public canAcceptExtension(extension: string) {
		return extension === "js";
	}

}
//register in load
this.registerView(
      SAMPLE_VIEW_TYPE,
      (leaf) => new SampleView(leaf, this)
    )
this.registerExtensions(["js"], "sample-view")

You can also refer to the code on the github. That's what I'm referring to here, I don't understand part of the logic very well, hope it helps.

windily-cloud avatar Oct 17 '22 09:10 windily-cloud

Thanks for the detailed reply @windily-cloud! My .config/ folder is actually in the vault, storing some config files for plugins like .vimrc. But yeah it's a good idea to store it in a non-hidden folder so you can edit it directly in Obsidian.

zcysxy avatar Oct 17 '22 15:10 zcysxy

Obsidian does not appear to emit a modify event when a file in a hidden folder (such as .config/) is edited, so I'm not sure how I would watch for changes to the snippet file.

I can add a feature to latex-suite that can open the snippets, I save it as a js file and it has syntax highlighting. Something like this. The code is not much, if @artisticat1 is willing to add it would be better.

That looks nice! You're welcome to submit a PR if you want. I'd be happy to accept it if the custom view only applies to the snippet file.

artisticat1 avatar Oct 18 '22 21:10 artisticat1

Obsidian does not appear to emit a modify event when a file in a hidden folder (such as .config/) is edited, so I'm not sure how I would watch for changes to the snippet file.

You are right. Plugins like obsidian-vimrc that can read files in a hidden folder need reloading to take effect after editing.

zcysxy avatar Oct 18 '22 22:10 zcysxy

That looks nice! You're welcome to submit a PR if you want. I'd be happy to accept it if the custom view only applies to the snippet file.

I submit a PR #69 . Now I can edit and load snippets in real time in obsidian, code highlighting and vim mode are also supported.

windily-cloud avatar Oct 19 '22 03:10 windily-cloud