PluginProbe
Code Snippets / 3.8.1
Code Snippets v3.8.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / js / mce.ts

mce.ts in Code Snippets 3.8.1, at js/mce.ts

105 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import tinymce from 'tinymce'
2 import type { Editor } from 'tinymce'
3 import type { ContentShortcodeAtts, SourceShortcodeAtts } from './types/Shortcodes'
4 import type { LocalisedEditor } from './types/WordPressEditor'
5
6 const convertToValues = (array: Record<string, string>) =>
7 Object.keys(array).map(key => ({
8 text: array[Number(key)],
9 value: key
10 }))
11
12 export const insertContentMenu = (editor: Editor, activeEditor: LocalisedEditor) => ({
13 text: activeEditor.getLang('code_snippets.insert_source_menu'),
14 onclick: () => {
15 editor.windowManager.open({
16 title: activeEditor.getLang('code_snippets.insert_source_title'),
17 body: [
18 {
19 type: 'listbox',
20 name: 'id',
21 label: activeEditor.getLang('code_snippets.snippet_label'),
22 values: convertToValues(<Record<string, string>> activeEditor.getLang('code_snippets.all_snippets'))
23 },
24 {
25 type: 'checkbox',
26 name: 'line_numbers',
27 label: activeEditor.getLang('code_snippets.show_line_numbers_label')
28 }
29 ],
30 onsubmit: (event: { data: SourceShortcodeAtts }) => {
31 const id = parseInt(event.data.id, 10)
32 if (!id) {
33 return
34 }
35
36 let atts = ''
37
38 if (event.data.line_numbers) {
39 atts += ' line_numbers=true'
40 }
41
42 editor.insertContent(`[code_snippet_source id=${id}${atts}]`)
43 }
44 }, {})
45 }
46 })
47
48 export const insertSourceMenu = (editor: Editor, ed: LocalisedEditor) => ({
49 text: ed.getLang('code_snippets.insert_content_menu'),
50 onclick: () => {
51 editor.windowManager.open({
52 title: ed.getLang('code_snippets.insert_content_title'),
53 body: [
54 {
55 type: 'listbox',
56 name: 'id',
57 label: ed.getLang('code_snippets.snippet_label'),
58 values: convertToValues(<Record<string, string>> ed.getLang('code_snippets.content_snippets'))
59 },
60 {
61 type: 'checkbox',
62 name: 'php',
63 label: ed.getLang('code_snippets.php_att_label')
64 },
65 {
66 type: 'checkbox',
67 name: 'format',
68 label: ed.getLang('code_snippets.format_att_label')
69 },
70 {
71 type: 'checkbox',
72 name: 'shortcodes',
73 label: ed.getLang('code_snippets.shortcodes_att_label')
74 }
75 ],
76 onsubmit: (event: { data: ContentShortcodeAtts }) => {
77 const id = parseInt(event.data.id, 10)
78 if (!id) {
79 return
80 }
81
82 let atts = ''
83
84 for (const [opt, val] of Object.entries(event.data)) {
85 if ('id' !== opt && val) {
86 atts += ` ${opt}=${val}`
87 }
88 }
89
90 editor.insertContent(`[code_snippet id=${id}${atts}]`)
91 }
92 }, {})
93 }
94 })
95
96 tinymce.PluginManager.add('code_snippets', editor => {
97 const activeEditor = <LocalisedEditor> tinymce.activeEditor
98
99 editor.addButton('code_snippets', {
100 icon: 'code',
101 menu: [insertContentMenu(editor, activeEditor), insertSourceMenu(editor, activeEditor)],
102 type: 'menubutton'
103 })
104 })
105