BUTTON[button-addlog, button-addnote]

label: Add Log
icon: edit
style: primary
id: button-addlog
hidden: true
actions:
  - type: inlineJS
    code: |
      const editor = app.workspace.activeEditor.editor;
      const content = editor.getValue();
      const lines = content.split('\n');
      
      // Find Interactions section
      const interactionsIndex = lines.findIndex(line => line.trim() === '## Interactions');
      
      if (interactionsIndex !== -1) {
        const today = moment().format('YYYY-MM-DD');
        
        // Check if today's entry already exists
        let todayEntryIndex = -1;
        for (let i = interactionsIndex + 1; i < lines.length; i++) {
          if (lines[i].trim() === '### ' + today) {
            todayEntryIndex = i;
            break;
          }
          // Stop if we hit another major section
          if (lines[i].startsWith('## ')) break;
        }
        
        let targetLine;
        if (todayEntryIndex === -1) {
          // Create new date entry
          const newEntry = '\n### ' + today + '\n- ';
          lines.splice(interactionsIndex + 1, 0, newEntry);
          editor.setValue(lines.join('\n'));
          targetLine = interactionsIndex + 3;
        } else {
          // Find the last bullet point under today's entry
          let lastBulletIndex = todayEntryIndex;
          for (let i = todayEntryIndex + 1; i < lines.length; i++) {
            if (lines[i].startsWith('- ')) {
              lastBulletIndex = i;
            } else if (lines[i].startsWith('#') || (lines[i].trim() === '' && i > todayEntryIndex + 1)) {
              break;
            }
          }
          // Add new bullet after the last one
          lines.splice(lastBulletIndex + 1, 0, '- ');
          editor.setValue(lines.join('\n'));
          targetLine = lastBulletIndex + 1;
        }
        
        // Move cursor and scroll into view
        editor.setCursor({line: targetLine, ch: 2});
        editor.scrollIntoView({line: targetLine, ch: 0}, 100);
        
        // Switch to source mode
        const view = app.workspace.activeLeaf.view;
        if (view.getMode && view.getMode() !== 'source') {
          await view.setMode('source');
        }
        
        // Focus the editor first
        editor.focus();
        
        // Try multiple methods to enter insert mode for vim
        setTimeout(() => {
          // Method 1: Direct vim state manipulation
          if (editor.cm && editor.cm.state && editor.cm.state.vim) {
            editor.cm.state.vim.insertMode = true;
            editor.cm.state.vim.visualMode = false;
            CodeMirror.signal(editor.cm, "vim-mode-change", {mode: "insert"});
          }
          
          // Method 2: Try using the vim API directly
          if (window.CodeMirrorAdapter && window.CodeMirrorAdapter.Vim) {
            window.CodeMirrorAdapter.Vim.handleKey(editor.cm, 'i');
          }
          
          // Method 3: Trigger 'i' key event
          const event = new KeyboardEvent('keydown', {
            key: 'i',
            code: 'KeyI',
            keyCode: 73,
            which: 73,
            bubbles: true
          });
          editor.containerEl.dispatchEvent(event);
        }, 100);
      }
label: Add Note
icon: sticky-note
style: default
id: button-addnote
hidden: true
actions:
  - type: inlineJS
    code: |
      const editor = app.workspace.activeEditor.editor;
      const content = editor.getValue();
      const lines = content.split('\n');
      
      // Find Notes section
      const notesIndex = lines.findIndex(line => line.trim() === '## Notes');
      
      if (notesIndex !== -1) {
        const timestamp = moment().format('YYYY-MM-DD HH:mm');
        const newNote = '- ' + timestamp + ': ';
        
        // Find where to insert
        let insertIndex = notesIndex + 1;
        
        // Skip the dataview line if it exists
        if (lines[notesIndex + 1] && lines[notesIndex + 1].trim().startsWith('`=')) {
          insertIndex = notesIndex + 2;
        }
        
        // Find the last note entry if any exist
        let hasExistingNotes = false;
        let lastNoteIndex = insertIndex - 1;
        for (let i = insertIndex; i < lines.length; i++) {
          if (lines[i].startsWith('- ')) {
            lastNoteIndex = i;
            hasExistingNotes = true;
          } else if (lines[i].startsWith('## ')) {
            break;
          }
        }
        
        // Insert the new note
        if (hasExistingNotes) {
          // Add after the last existing note
          lines.splice(lastNoteIndex + 1, 0, newNote);
          insertIndex = lastNoteIndex + 1;
        } else {
          // First note - add blank line before it if needed
          if (lines[insertIndex] && lines[insertIndex].trim() !== '') {
            lines.splice(insertIndex, 0, '', newNote);
            insertIndex++;
          } else {
            lines.splice(insertIndex, 0, newNote);
          }
        }
        
        editor.setValue(lines.join('\n'));
        
        // Move cursor and scroll into view
        editor.setCursor({line: insertIndex, ch: newNote.length});
        editor.scrollIntoView({line: insertIndex, ch: 0}, 100);
        
        // Switch to source mode
        const view = app.workspace.activeLeaf.view;
        if (view.getMode && view.getMode() !== 'source') {
          await view.setMode('source');
        }
        
        // Focus the editor first
        editor.focus();
        
        // Try multiple methods to enter insert mode for vim
        setTimeout(() => {
          // Method 1: Direct vim state manipulation
          if (editor.cm && editor.cm.state && editor.cm.state.vim) {
            editor.cm.state.vim.insertMode = true;
            editor.cm.state.vim.visualMode = false;
            CodeMirror.signal(editor.cm, "vim-mode-change", {mode: "insert"});
          }
          
          // Method 2: Try using the vim API directly
          if (window.CodeMirrorAdapter && window.CodeMirrorAdapter.Vim) {
            window.CodeMirrorAdapter.Vim.handleKey(editor.cm, 'i');
          }
          
          // Method 3: Trigger 'i' key event
          const event = new KeyboardEvent('keydown', {
            key: 'i',
            code: 'KeyI',
            keyCode: 73,
            which: 73,
            bubbles: true
          });
          editor.containerEl.dispatchEvent(event);
        }, 100);
      }