summaryrefslogtreecommitdiffstats
path: root/web_src/js/features/comp/EasyMDEToolbarActions.js
blob: 35abb877d7fcf4187b7ba109e320c3e5b7bd0c3c (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {svg} from '../../svg.js';

export function easyMDEToolbarActions(EasyMDE, editor) {
  const actions = {
    '|': '|',
    'heading-1': {
      action: EasyMDE.toggleHeading1,
      icon: svg('octicon-heading'),
      title: 'Heading 1',
    },
    'heading-2': {
      action: EasyMDE.toggleHeading2,
      icon: svg('octicon-heading'),
      title: 'Heading 2',
    },
    'heading-3': {
      action: EasyMDE.toggleHeading3,
      icon: svg('octicon-heading'),
      title: 'Heading 3',
    },
    'heading-smaller': {
      action: EasyMDE.toggleHeadingSmaller,
      icon: svg('octicon-heading'),
      title: 'Decrease Heading',
    },
    'heading-bigger': {
      action: EasyMDE.toggleHeadingBigger,
      icon: svg('octicon-heading'),
      title: 'Increase Heading',
    },
    'bold': {
      action: EasyMDE.toggleBold,
      icon: svg('octicon-bold'),
      title: 'Bold',
    },
    'italic': {
      action: EasyMDE.toggleItalic,
      icon: svg('octicon-italic'),
      title: 'Italic',
    },
    'strikethrough': {
      action: EasyMDE.toggleStrikethrough,
      icon: svg('octicon-strikethrough'),
      title: 'Strikethrough',
    },
    'quote': {
      action: EasyMDE.toggleBlockquote,
      icon: svg('octicon-quote'),
      title: 'Quote',
    },
    'code': {
      action: EasyMDE.toggleCodeBlock,
      icon: svg('octicon-code'),
      title: 'Code',
    },
    'link': {
      action: EasyMDE.drawLink,
      icon: svg('octicon-link'),
      title: 'Link',
    },
    'unordered-list': {
      action: EasyMDE.toggleUnorderedList,
      icon: svg('octicon-list-unordered'),
      title: 'Unordered List',
    },
    'ordered-list': {
      action: EasyMDE.toggleOrderedList,
      icon: svg('octicon-list-ordered'),
      title: 'Ordered List',
    },
    'image': {
      action: EasyMDE.drawImage,
      icon: svg('octicon-image'),
      title: 'Image',
    },
    'table': {
      action: EasyMDE.drawTable,
      icon: svg('octicon-table'),
      title: 'Table',
    },
    'horizontal-rule': {
      action: EasyMDE.drawHorizontalRule,
      icon: svg('octicon-horizontal-rule'),
      title: 'Horizontal Rule',
    },
    'preview': {
      action: EasyMDE.togglePreview,
      icon: svg('octicon-eye'),
      title: 'Preview',
    },
    'fullscreen': {
      action: EasyMDE.toggleFullScreen,
      icon: svg('octicon-screen-full'),
      title: 'Fullscreen',
    },
    'side-by-side': {
      action: EasyMDE.toggleSideBySide,
      icon: svg('octicon-columns'),
      title: 'Side by Side',
    },

    // Forgejo custom actions
    'gitea-checkbox-empty': {
      action(e) {
        const cm = e.codemirror;
        cm.replaceSelection(`\n- [ ] ${cm.getSelection()}`);
        cm.focus();
      },
      icon: svg('gitea-empty-checkbox'),
      title: 'Add Checkbox (empty)',
    },
    'gitea-checkbox-checked': {
      action(e) {
        const cm = e.codemirror;
        cm.replaceSelection(`\n- [x] ${cm.getSelection()}`);
        cm.focus();
      },
      icon: svg('octicon-checkbox'),
      title: 'Add Checkbox (checked)',
    },
    'gitea-switch-to-textarea': {
      action: () => {
        editor.userPreferredEditor = 'textarea';
        editor.switchToTextarea();
      },
      icon: svg('octicon-arrow-switch'),
      title: 'Revert to simple textarea',
    },
    'gitea-code-inline': {
      action(e) {
        const cm = e.codemirror;
        const selection = cm.getSelection();
        cm.replaceSelection(`\`${selection}\``);
        if (!selection) {
          const cursorPos = cm.getCursor();
          cm.setCursor(cursorPos.line, cursorPos.ch - 1);
        }
        cm.focus();
      },
      icon: svg('octicon-chevron-right'),
      title: 'Add Inline Code',
    },
  };

  for (const [key, value] of Object.entries(actions)) {
    if (typeof value !== 'string') {
      value.name = key;
    }
  }

  return actions;
}