PluginProbe
Code Snippets / 3.10.1
Code Snippets v3.10.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.10.1, at js/mce.ts

133 lines 4.5 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 // Custom scissors icon as base64-encoded SVG (same as used in WP admin menu)
97 // Base64-encoded version of menu-icon.svg
98 const scissorsIcon =
99 'data:image/svg+xml;base64,' +
100 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGZpbGw9Im5vbmUiIHZpZX' +
101 'dCb3g9IjAgMCAyNiAyNSI+PHBhdGggZmlsbD0iIzUxNTc1ZiIgZD0iTTYuMTI3IDExLjk2Nmgz' +
102 'LjQxNGEuOTEuOTEgMCAwIDEgLjg0NC41NjMuOTMuOTMgMCAwIDEtLjE4NSAxLjAwNGwuMDA1Lj' +
103 'AxLTIuMzM4IDIuMzU1YTQuMTkgNC4xOSAwIDAgMCAwIDUuODg1QTQuMTEgNC4xMSAwIDAgMCAx' +
104 'MC43ODQgMjNhNC4xMSA0LjExIDAgMCAwIDIuOTE3LTEuMjE3IDQuMiA0LjIgMCAwIDAgMS4wND' +
105 'gtMS44MDIgNC4yIDQuMiAwIDAgMC0uOTE1LTMuOTQgNC4xIDQuMSAwIDAgM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.PluginManager.add('code_snippets', editor => {
121 const activeEditor = <LocalisedEditor> tinymce.activeEditor
122
123 editor.addButton('code_snippets', {
124 type: 'menubutton',
125 title: 'Code Snippets',
126 image: scissorsIcon,
127 menu: [
128 insertContentMenu(editor, activeEditor),
129 insertSourceMenu(editor, activeEditor)
130 ],
131 })
132 })
133