All files / app/mods draft.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 0/0
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87117x   117x       117x                                                 117x                                                                                                 117x            
import { DateTime } from 'luxon';
import { Plugin } from '../model/plugin';
import { refSchema } from '../model/ref';
import { Mod } from '../model/tag';
import { Template } from '../model/template';
 
export const editingPlugin: Plugin = {
  tag: 'plugin/editing',
  name: $localize`📝️ Editing`,
  config: {
    mod: $localize`🎬️ Drafts`,
    version: 1,
    type: 'plugin',
    default: true,
    view: $localize`📝️`,
    inbox: $localize`drafts`,
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    description: $localize`Save edits to a plugin before publishing.`,
    embeddable: true,
    icons: [{ thumbnail: $localize`📝️`, order: 1 }],
    filters: [
      { query: 'plugin/editing', label: $localize`📝️ editing`, title: $localize`Drafts`, group: $localize`Plugins 🧰️` },
    ],
    actions: [
      { tag: 'plugin/editing', labelOn: $localize`revert`, title: $localize`Revert pending edits to this Ref`, confirm: $localize`Are you sure?` },
      { tag: 'plugin/delta/commit', labelOff: $localize`commit`, title: $localize`Save pending edits to this Ref`, confirm: $localize`Are you sure?` },
    ],
  },
  schema: refSchema,
};
 
export const commitPlugin: Plugin = {
  tag: 'plugin/delta/commit',
  name: $localize`🐢️ Commit`,
  config: {
    mod: $localize`🎬️ Drafts`,
    version: 1,
    type: 'plugin',
    default: true,
    actions: [
      { tag: 'plugin/delta/commit', labelOn: $localize`cancel`, title: $localize`Cancel committing edits to this Ref` },
    ],
    timeoutMs: 30_000,
    language: 'javascript',
    // language=JavaScript
    script: `
      const axios = require('axios');
      const ref = JSON.parse(require('fs').readFileSync(0, 'utf-8'));
      if (ref?.tags?.find(t => t === 'plugin/editing' || t?.startsWith('plugin/editing/'))) {
        const editPlugin = ref.plugins['plugin/editing'];
        delete ref.plugins['plugin/editing'];
        delete ref.metadata;
        ref.tags = ref.tags
          .filter(t => t !== 'plugin/editing' && !t.startsWith('plugin/editing/'))
          .filter(t => t !== 'plugin/delta/commit' && !t.startsWith('plugin/delta/commit/'));
        const edit = {
          ...ref,
          ...editPlugin,
          url: ref.url,
        };
        delete edit.metadata;
        const author = ref.tags.find(tag => tag === '+user' || tag === '_user' || tag.startsWith('+user/') || tag.startsWith('_user/'));
        if (!author) throw new Error('No author tag found');
        await axios.post(process.env.JASPER_API + '/pub/api/v1/repl/ref', [edit], {
          headers: {
            'Local-Origin': ref.origin || 'default',
            'User-Tag': author,
          },
          params: {
            origin: ref.origin,
          }
        }).catch(e => {
          console.error(e.response.data);
          throw new Error(e);
        });
      }
    `,
  }
}
 
export const draftMod: Mod = {
  plugin: [
    editingPlugin,
    commitPlugin,
  ],
};