Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] Saving Snippets in a Hidden Folder #67

Open
zcysxy opened this issue Oct 16, 2022 · 5 comments
Open

[FR] Saving Snippets in a Hidden Folder #67

zcysxy opened this issue Oct 16, 2022 · 5 comments

Comments

@zcysxy
Copy link

zcysxy commented Oct 16, 2022

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

@windily-cloud
Copy link

windily-cloud commented Oct 17, 2022

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.

@zcysxy
Copy link
Author

zcysxy commented Oct 17, 2022

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.

@artisticat1
Copy link
Owner

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.

@zcysxy
Copy link
Author

zcysxy commented Oct 18, 2022

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.

@windily-cloud
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants