koblog/bl-plugins/easymde/plugin.php

120 lines
3.3 KiB
PHP
Raw Normal View History

2019-05-25 12:14:49 +02:00
<?php
2021-03-09 15:53:36 +01:00
class pluginEasyMDE extends Plugin {
2019-05-25 12:14:49 +02:00
private $loadOnViews = array(
2021-03-09 15:53:36 +01:00
'editor' // Load this plugin only in the Editor view
2019-05-25 12:14:49 +02:00
);
public function init()
{
$this->dbFields = array(
'tabSize'=>'2',
'toolbar'=>'"bold", "italic", "heading", "|", "quote", "unordered-list", "|", "link", "image", "code", "horizontal-rule", "|", "preview", "side-by-side", "fullscreen"',
'spellChecker'=>true
);
}
public function form()
{
global $L;
2021-03-09 15:53:36 +01:00
$html = '<div class="mb-3">';
$html .= '<label class="form-label" for="toolbar">'.$L->get('toolbar').'</label>';
$html .= '<input class="form-control" name="toolbar" id="toolbar" type="text" value="'.$this->getValue('toolbar').'">';
2019-05-25 12:14:49 +02:00
$html .= '</div>';
2021-03-09 15:53:36 +01:00
$html .= '<div class="mb-3">';
$html .= '<label class="form-label" for="tabSize">'.$L->get('tab-size').'</label>';
$html .= '<input class="form-control" name="tabSize" id="tabSize" type="text" value="'.$this->getValue('tabSize').'">';
2019-05-25 12:14:49 +02:00
$html .= '</div>';
2021-03-09 15:53:36 +01:00
$html .= '<div class="mb-3">';
$html .= '<label class="form-label" for="spellChecker">'.$L->get('spell-checker').'</label>';
$html .= '<select class="form-control" name="spellChecker">';
2019-05-25 12:14:49 +02:00
$html .= '<option value="true" '.($this->getValue('spellChecker')===true?'selected':'').'>'.$L->get('enabled').'</option>';
$html .= '<option value="false" '.($this->getValue('spellChecker')===false?'selected':'').'>'.$L->get('disabled').'</option>';
$html .= '</select>';
$html .= '</div>';
return $html;
}
public function adminHead()
{
2021-03-09 15:53:36 +01:00
// Load the plugin only in the controllers setted in $this->loadOnController
2019-05-25 12:14:49 +02:00
if (!in_array($GLOBALS['ADMIN_VIEW'], $this->loadOnViews)) {
return false;
}
$html = $this->includeCSS('easymde.min.css');
$html .= $this->includeCSS('bludit.css');
2021-03-09 15:53:36 +01:00
$html .= $this->includeJS('easymde.min.js');
2019-05-25 12:14:49 +02:00
return $html;
}
public function adminBodyEnd()
{
2021-03-09 15:53:36 +01:00
global $L;
// Load the plugin only in the controllers setted in $this->loadOnController
2019-05-25 12:14:49 +02:00
if (!in_array($GLOBALS['ADMIN_VIEW'], $this->loadOnViews)) {
return false;
}
$langImage = $L->g('Image description');
$spellCheckerEnable = $this->getValue('spellChecker')?'true':'false';
$tabSize = $this->getValue('tabSize');
$toolbar = Sanitize::htmlDecode($this->getValue('toolbar'));
$pageBreak = PAGE_BREAK;
2021-06-08 23:34:49 +02:00
return <<<EOF
<script>
// Function required for Bludit
// Returns the content of the editor
function editorGetContent() {
return easymde.value();
}
// Function required for Bludit
// Insert HTML content at the cursor position
function editorInsertContent(html, type='') {
var text = easymde.value();
if (type == 'image') {
easymde.value(text + "![$langImage]("+filename+")" + "\\n");
} else {
easymde.value(html + "\\n");
}
easymde.codemirror.refresh();
}
var easymde = new EasyMDE({
element: document.getElementById("editor"),
status: false,
toolbarTips: true,
toolbarGuideIcon: true,
autofocus: false,
placeholder: "",
lineWrapping: true,
autoDownloadFontAwesome: false,
indentWithTabs: true,
tabSize: $tabSize,
spellChecker: $spellCheckerEnable,
toolbar: [$toolbar,
"|",
{
name: "pageBreak",
action: function addPageBreak(editor){
var cm = editor.codemirror;
output = "$pageBreak";
cm.replaceSelection(output);
},
2021-06-14 23:56:04 +02:00
className: "bi-file-earmark-break",
2021-06-08 23:34:49 +02:00
title: "Page break",
}]
});
</script>
EOF;
2019-05-25 12:14:49 +02:00
}
}