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