| 1 |
/* global tinymce */ |
| 2 |
// Minimal TinyMCE plugin to manage alignment of FIGURE elements across wpForo editors |
| 3 |
// This provides the same alignment button behavior previously bundled in wpForo Advanced Attachments |
| 4 |
// and applies to any <figure> selected in the editor (attachments, GIPHY, Tenor, etc.). |
| 5 |
|
| 6 |
(function() { |
| 7 |
tinymce.PluginManager.add('wpfaeditimage', function (editor) { |
| 8 |
var toolbar; |
| 9 |
var each = tinymce.each; |
| 10 |
|
| 11 |
function isPlaceholder(node) { |
| 12 |
return !!(editor.dom.getAttrib(node, 'data-mce-placeholder') || editor.dom.getAttrib(node, 'data-mce-object')); |
| 13 |
} |
| 14 |
|
| 15 |
function isFigure(el) { |
| 16 |
return el && el.nodeName === 'FIGURE'; |
| 17 |
} |
| 18 |
|
| 19 |
// Add alignment buttons similar to WP core naming |
| 20 |
each({ |
| 21 |
alignleft: 'Align left', |
| 22 |
aligncenter: 'Align center', |
| 23 |
alignright: 'Align right', |
| 24 |
alignnone: 'No alignment' |
| 25 |
}, function (tooltip, name) { |
| 26 |
var direction = name.slice(5); |
| 27 |
|
| 28 |
editor.addButton('wp_img_' + name, { |
| 29 |
tooltip: tooltip, |
| 30 |
icon: 'dashicon dashicons-align-' + direction, |
| 31 |
cmd: ('alignnone' === name) ? 'wpAlignNone' : 'Justify' + direction.charAt(0).toUpperCase() + direction.slice(1), |
| 32 |
onPostRender: function () { |
| 33 |
var self = this; |
| 34 |
editor.on('NodeChange', function (event) { |
| 35 |
if (!isFigure(event.element)) return; |
| 36 |
var node = editor.dom.getParent(event.element, '.wp-caption') || event.element; |
| 37 |
if ('alignnone' === name) { |
| 38 |
self.active(!/\balign(left|center|right)\b/.test(node.className)); |
| 39 |
} else { |
| 40 |
self.active(editor.dom.hasClass(node, name)); |
| 41 |
} |
| 42 |
}); |
| 43 |
} |
| 44 |
}); |
| 45 |
}); |
| 46 |
|
| 47 |
// Remove currently selected figure |
| 48 |
function removeSelectedFigure() { |
| 49 |
var node = editor.selection.getNode(); |
| 50 |
var figure = editor.dom.getParent(node, 'figure') || (isFigure(node) ? node : null); |
| 51 |
if (!figure) return; |
| 52 |
|
| 53 |
// Place caret after the figure if possible |
| 54 |
var next = figure.nextSibling; |
| 55 |
var prev = figure.previousSibling; |
| 56 |
var parent = figure.parentNode; |
| 57 |
|
| 58 |
editor.dom.remove(figure); |
| 59 |
|
| 60 |
// Try to move selection to a sensible place |
| 61 |
if (next) { |
| 62 |
editor.selection.select(next, true); |
| 63 |
editor.selection.collapse(true); |
| 64 |
} else if (prev) { |
| 65 |
editor.selection.select(prev, true); |
| 66 |
editor.selection.collapse(false); |
| 67 |
} else if (parent) { |
| 68 |
editor.selection.select(parent, true); |
| 69 |
editor.selection.collapse(false); |
| 70 |
} |
| 71 |
|
| 72 |
editor.nodeChanged(); |
| 73 |
} |
| 74 |
|
| 75 |
// Add delete button |
| 76 |
editor.addButton('wp_img_remove', { |
| 77 |
tooltip: 'Remove', |
| 78 |
icon: 'dashicon dashicons-no', |
| 79 |
onclick: removeSelectedFigure, |
| 80 |
onPostRender: function () { |
| 81 |
var self = this; |
| 82 |
editor.on('NodeChange', function (event) { |
| 83 |
self.disabled(!isFigure(event.element)); |
| 84 |
}); |
| 85 |
} |
| 86 |
}); |
| 87 |
|
| 88 |
// Build inline toolbar |
| 89 |
editor.once('preinit', function () { |
| 90 |
if (editor.wp && editor.wp._createToolbar) { |
| 91 |
toolbar = editor.wp._createToolbar([ |
| 92 |
'wp_img_alignleft', |
| 93 |
'wp_img_aligncenter', |
| 94 |
'wp_img_alignright', |
| 95 |
'wp_img_alignnone', |
| 96 |
'wp_img_remove' |
| 97 |
]); |
| 98 |
} |
| 99 |
}); |
| 100 |
|
| 101 |
// Show toolbar when FIGURE is selected |
| 102 |
editor.on('wptoolbar', function (event) { |
| 103 |
if (isFigure(event.element) && !isPlaceholder(event.element)) { |
| 104 |
event.toolbar = toolbar; |
| 105 |
} |
| 106 |
}); |
| 107 |
|
| 108 |
// Commands for toggling classes on FIGURE |
| 109 |
function setAlign(align) { |
| 110 |
var node = editor.selection.getNode(); |
| 111 |
var figure = editor.dom.getParent(node, 'figure') || (isFigure(node) ? node : null); |
| 112 |
if (!figure) return false; |
| 113 |
editor.dom.removeClass(figure, 'alignleft'); |
| 114 |
editor.dom.removeClass(figure, 'aligncenter'); |
| 115 |
editor.dom.removeClass(figure, 'alignright'); |
| 116 |
if (align) editor.dom.addClass(figure, 'align' + align); |
| 117 |
editor.nodeChanged(); |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
function isFigureSelected() { |
| 122 |
var node = editor.selection.getNode(); |
| 123 |
return !!(editor.dom.getParent(node, 'figure') || isFigure(node)); |
| 124 |
} |
| 125 |
|
| 126 |
function alignText(side) { |
| 127 |
if (side) { |
| 128 |
editor.formatter.toggle('align' + side); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
function alignNoneText() { |
| 133 |
editor.formatter.remove('alignleft'); |
| 134 |
editor.formatter.remove('aligncenter'); |
| 135 |
editor.formatter.remove('alignright'); |
| 136 |
editor.nodeChanged(); |
| 137 |
} |
| 138 |
|
| 139 |
editor.addCommand('JustifyLeft', function () { |
| 140 |
if (!setAlign('left')) { |
| 141 |
alignText('left'); |
| 142 |
} |
| 143 |
}); |
| 144 |
editor.addCommand('JustifyCenter', function () { |
| 145 |
if (!setAlign('center')) { |
| 146 |
alignText('center'); |
| 147 |
} |
| 148 |
}); |
| 149 |
editor.addCommand('JustifyRight', function () { |
| 150 |
if (!setAlign('right')) { |
| 151 |
alignText('right'); |
| 152 |
} |
| 153 |
}); |
| 154 |
editor.addCommand('wpAlignNone', function () { |
| 155 |
if (!setAlign('')) { |
| 156 |
alignNoneText(); |
| 157 |
} |
| 158 |
}); |
| 159 |
}); |
| 160 |
})(); |
| 161 |
|