-
Notifications
You must be signed in to change notification settings - Fork 29
/
TinyMCEPlugin.php
135 lines (120 loc) · 3.67 KB
/
TinyMCEPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* @file plugins/generic/tinymce/TinyMCEPlugin.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class TinyMCEPlugin
*
* @ingroup plugins_generic_tinymce
*
* @brief TinyMCE WYSIWYG plugin for textareas - to allow cross-browser HTML editing
*/
namespace APP\plugins\generic\tinymce;
use APP\core\Application;
use PKP\config\Config;
use PKP\core\Registry;
use PKP\facades\Locale;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class TinyMCEPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null): bool
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
Hook::add('TemplateManager::display', $this->registerJS(...));
}
return true;
}
return false;
}
/**
* @copydoc Plugin::getContextSpecificPluginSettingsFile()
*/
public function getContextSpecificPluginSettingsFile(): string
{
return $this->getPluginPath() . '/settings.xml';
}
/**
* @copydoc Plugin::getInstallSitePluginSettingsFile()
*/
public function getInstallSitePluginSettingsFile(): string
{
return $this->getPluginPath() . '/settings.xml';
}
/**
* Determine whether the plugin can be disabled.
*/
public function getCanDisable(): bool
{
return false;
}
/**
* Register the TinyMCE JavaScript file
*
* Hooked to the the `display` callback in TemplateManager
*/
public function registerJS(string $hookName, array $args): bool
{
$request = & Registry::get('request');
$templateManager = & $args[0];
// Load the TinyMCE JavaScript file
$min = Config::getVar('general', 'enable_minified') ? '.min' : '';
$templateManager->addJavaScript(
'tinymce',
$request->getBaseUrl() . '/lib/pkp/lib/vendor/tinymce/tinymce/tinymce' . $min . '.js',
[
'contexts' => 'backend',
]
);
// Load the script data used by the JS library
$data = [];
$localeKey = substr(Locale::getLocale(), 0, 2);
$localePath = $request->getBaseUrl() . '/plugins/generic/tinymce/langs/' . $localeKey . '.js';
if (file_exists($localePath)) {
$data['tinymceParams'] = [
'language' => $localeKey,
'language_url' => $localePath,
];
}
$context = $request->getContext();
if ($context) {
$data['uploadUrl'] = $request->getDispatcher()->url($request, Application::ROUTE_API, $context->getPath(), '_uploadPublicFile');
}
$templateManager->addJavaScript(
'tinymceData',
'$.pkp.plugins.generic = $.pkp.plugins.generic || {};' .
'$.pkp.plugins.generic.tinymceplugin = ' . json_encode($data) . ';',
[
'inline' => true,
'contexts' => 'backend',
]
);
return false;
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName()
{
return __('plugins.generic.tinymce.name');
}
/**
* @copydoc Plugin::getDescription()
*/
public function getDescription()
{
return __('plugins.generic.tinymce.description');
}
}
if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\generic\tinymce\TinyMCEPlugin', '\TinyMCEPlugin');
}