-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
140 lines (124 loc) · 3.78 KB
/
controller.php
File metadata and controls
140 lines (124 loc) · 3.78 KB
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
136
137
138
139
140
<?php
namespace Concrete\Package\DarkModeEditor;
use Concrete\Core\Application\Application;
use Concrete\Core\Asset\AssetList;
use Concrete\Core\Editor\CkeditorEditor;
use Concrete\Core\Editor\Plugin as EditorPlugin;
use Concrete\Core\Package\Package;
use Concrete\Core\Routing\RouterInterface;
defined('C5_EXECUTE') or die('Access Denied.');
class Controller extends Package
{
protected $pkgHandle = 'dark_mode_editor';
protected $pkgVersion = '1.2.0';
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::$appVersionRequired
*/
protected $appVersionRequired = '8.5.7';
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::getPackageName()
*/
public function getPackageName()
{
return t('CKEditor Dark Mode Toggle');
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::getPackageDescription()
*/
public function getPackageDescription()
{
return t('Adds a Dark Mode Toggle to CKEditor');
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::install()
*/
public function install()
{
$package = parent::install();
$this->setPluginEnabled(true);
return $package;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Package::uninstall()
*/
public function uninstall()
{
parent::uninstall();
$this->setPluginEnabled(false);
}
public function on_start()
{
$this->registerAssets();
$this->registerPlugin();
}
private function registerAssets()
{
$router = $this->app->make(RouterInterface::class);
$router->get('ccm/dark_mode_editor/ckeditor4/plugin', Plugin::class . '::view');
$al = AssetList::getInstance();
$al->register('javascript-localized', 'dark_mode_editor/ckeditor4/plugin', 'ccm/dark_mode_editor/ckeditor4/plugin');
$al->registerGroup('dark_mode_editor/ckeditor4/plugin', [
['javascript-localized', 'dark_mode_editor/ckeditor4/plugin'],
]);
}
private function registerPlugin()
{
$this->app->extend(
CkeditorEditor::class,
static function (CkeditorEditor $editor, Application $app) {
$pluginManager = $editor->getPluginManager();
$plugin = new EditorPlugin();
$plugin->setKey('darkmode');
$plugin->setName('Darkmode Toggle');
$plugin->requireAsset('dark_mode_editor/ckeditor4/plugin');
if (!$pluginManager->isAvailable($plugin)) {
$pluginManager->register($plugin);
}
return $editor;
}
);
}
/**
* @param bool $enable
*/
private function setPluginEnabled($enable)
{
$editor = $this->app->make('editor');
if (!$editor instanceof CkeditorEditor) {
return;
}
$manager = $editor->getPluginManager();
$selected = array_values($manager->getSelectedPlugins());
if ($enable) {
if (in_array('darkmode', $selected, true)) {
return;
}
$selected[] = 'darkmode';
} else {
$index = array_search('darkmode', $selected, true);
if ($index === false) {
return;
}
do {
array_splice($selected, $index, 1);
$index = array_search('darkmode', $selected, true);
} while ($index !== false);
}
$site = $this->app->make('site')->getSite();
if (!$site) {
return;
}
$siteConfig = $site->getConfigRepository();
$siteConfig->save('editor.ckeditor4.plugins.selected', $selected);
}
}