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

Play specific Soundscape on File open #39

Open
pMat-19 opened this issue Apr 4, 2024 · 0 comments
Open

Play specific Soundscape on File open #39

pMat-19 opened this issue Apr 4, 2024 · 0 comments

Comments

@pMat-19
Copy link

pMat-19 commented Apr 4, 2024

Hi, I'm not great at programming Obsidian plugins but I thought I could ask you to add the following functionality to the plugin: start playing a specific Soundscape on File open. Here's what should be added (AI generated):

  1. Add a new setting to store the folder-to-soundscape mappings.
  2. Listen for the 'file-open' event and trigger a new method to handle the event.
  3. In the new method, get the folder of the opened file and check if there's a designated soundscape for that folder.
  4. If a soundscape is found, update the current soundscape and start playing it. If the soundscape is the same as the one that is already playing, then do nothing.

Here's the code to implement these steps:

// Step 1: Add a new setting to store folder-to-soundscape mappings
interface SoundscapesSettings {
  // ... (existing settings)
  folderSoundscapeMap: Record<string, string>;
}

const DEFAULT_SETTINGS: SoundscapesSettings = {
  // ... (existing default settings)
  folderSoundscapeMap: {},
};

// Step 2: Listen for the 'file-open' event and trigger a new method
async onload() {
  // ... (existing code)
  this.registerEvent(this.app.workspace.on('file-open', this.onFileOpen.bind(this)));
}

// Step 3: Implement the onFileOpen method
async onFileOpen(file: TAbstractFile) {
  if (file instanceof TFile) {
    const folder = file.parent.path;
    const soundscape = this.settings.folderSoundscapeMap[folder];

    // Step 4: If a soundscape is found, update the current soundscape and start playing
    if (soundscape) {
      this.settings.soundscape = soundscape;
      this.onSoundscapeChange();
    }
  }
}

// Add a method to update the folder-to-soundscape mappings
async updateFolderSoundscapeMap(folder: string, soundscape: string) {
  this.settings.folderSoundscapeMap[folder] = soundscape;
  await this.saveSettings();
}

With these changes:

  1. We added a new folderSoundscapeMap setting to store the mappings between folders and soundscapes.
  2. We registered an event listener for the 'file-open' event, which triggers the onFileOpen method.
  3. In the onFileOpen method, we get the folder of the opened file and check if there's a designated soundscape for that folder using the folderSoundscapeMap.
  4. If a soundscape is found, we update the current soundscape (this.settings.soundscape) and call onSoundscapeChange() to start playing it.
  5. We also added an updateFolderSoundscapeMap method to allow updating the folder-to-soundscape mappings. You can call this method whenever you want to associate a folder with a soundscape.

Note: Make sure to update the plugin's settings UI to allow users to configure the folder-to-soundscape mappings. You can use the updateFolderSoundscapeMap method to save the mappings when the user makes changes in the settings.

Thank you for your consideration!

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

1 participant