Init script to toggle set of tags

Continuing the discussion from Birch 1.5.0 Dev Release:

So as I said before, there’s still no general UI for editing tags. But if you wanted to start using tags here’s a script that can help. Copy and past it into your Atom > Open Your Init Script, then edit to create your own list of tags and shortcuts.

As the script stands now if you:

  1. command-shift-t then press 1 the current selection is tagged #home
  2. command-shift-t then press 2 the current selection is tagged #work
  3. command-shift-t then press 3 the current selection is tagged #store

Not a final solution, but I figured a good example of how you can hack Birch to do things through it’s API.

{Disposable, CompositeDisposable} = require 'atom'
atom.packages.serviceHub.consume 'birch-outine-editor-service', '1.0.0', (birchService) ->
  toggleTag = (tag) ->
    editor = birchService.getActiveOutlineEditor()
    selectedItems = editor.selection.items
    if selectedItems.length
      editor.outline.beginUpdates()
      if tag in (selectedItems[0].getAttribute('data-tags', true) or [])
        # removing
        for each in selectedItems
          tags = each.getAttribute('data-tags', true)
          if tags and tag in tags
            tags.splice tags.indexOf(tag), 1
            each.setAttribute 'data-tags', tags
      else
        # adding
        for each in selectedItems
          tags = each.getAttribute('data-tags', true) or []
          unless tag in tags
            tags.push tag
            each.setAttribute 'data-tags', tags
      editor.outline.endUpdates()

  tagsDisposable = new CompositeDisposable(
    atom.commands.add 'birch-outline-editor', 'tag-home', -> toggleTag('home')
    atom.commands.add 'birch-outline-editor', 'tag-work', -> toggleTag('work')
    atom.commands.add 'birch-outline-editor', 'tag-store', -> toggleTag('store')
    atom.keymap.add 'tags',
      'birch-outline-editor':
        'cmd-shift-t 1': 'tag-home'
        'cmd-shift-t 2': 'tag-work'
        'cmd-shift-t 3': 'tag-store'
  )

  new Disposable =>
    tagsDisposable.dispose()
    service = null