Skip to content

Commit

Permalink
ENHANCEMENT Lazy-load spellcheck config instead of every request
Browse files Browse the repository at this point in the history
Fixes #38
  • Loading branch information
Damian Mooyman committed Jun 17, 2018
1 parent 93ff00e commit 42d24df
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 29 deletions.
8 changes: 3 additions & 5 deletions _config/config.yml
Expand Up @@ -16,8 +16,6 @@ SilverStripe\Core\Injector\Injector:
---
Name: spellcheckprocessors
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\Director:
properties:
Middlewares:
SpellCheckMiddleware: '%$SilverStripe\SpellCheck\Handling\SpellCheckMiddleware'
SilverStripe\Admin\LeftAndMain:
extensions:
SpellCheckAdminExtension: SilverStripe\SpellCheck\Handling\SpellCheckAdminExtension
89 changes: 89 additions & 0 deletions src/Handling/SpellCheckAdminExtension.php
@@ -0,0 +1,89 @@
<?php

namespace SilverStripe\SpellCheck\Handling;

use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
use SilverStripe\i18n\i18n;
use SilverStripe\Security\SecurityToken;

/**
* Update html editor to enable spellcheck
*/
class SpellCheckAdminExtension extends Extension
{
use Configurable;

/**
* HTMLEditorConfig name to use
*
* @var string
* @config
*/
private static $editor = 'cms';

public function init()
{
// Set settings (respect deprecated middleware)
$editor = SpellCheckMiddleware::config()->get('editor')
?: static::config()->get('editor');

/** @var TinyMCEConfig $editorConfig */
$editorConfig = TinyMCEConfig::get($editor);

$editorConfig->enablePlugins('spellchecker');
$editorConfig->addButtonsToLine(2, 'spellchecker');

$token = SecurityToken::inst();

$editorConfig
->setOption('spellchecker_rpc_url', Director::absoluteURL($token->addToUrl('spellcheck/')))
->setOption('browser_spellcheck', false)
->setOption('spellchecker_languages', implode(',', $this->getLanguages()));

$defaultLocale = $this->getDefaultLocale();
if ($defaultLocale) {
$editorConfig->setOption('spellchecker_language', $defaultLocale);
}
}

/**
* Check languages to set
*
* @return string[]
*/
public function getLanguages()
{
$languages = [];
foreach (SpellController::get_locales() as $locale) {
$localeName = i18n::getData()->localeName($locale);
// Fix incorrectly spelled Māori language
$localeName = str_replace('Maori', 'Māori', $localeName);
$languages[] = $localeName . '=' . $locale;
}
return $languages;
}

/**
* Returns the default locale for TinyMCE. Either via configuration or the first in the list of locales.
*
* @return string|false
*/
public function getDefaultLocale()
{
// Check configuration first
$defaultLocale = SpellController::config()->get('default_locale');
if ($defaultLocale) {
return $defaultLocale;
}

// Grab the first one in the list
$locales = SpellController::get_locales();
if (empty($locales)) {
return false;
}
return reset($locales);
}
}
27 changes: 3 additions & 24 deletions src/Handling/SpellCheckMiddleware.php
Expand Up @@ -2,15 +2,15 @@

namespace SilverStripe\SpellCheck\Handling;

use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;
use SilverStripe\i18n\i18n;
use SilverStripe\Security\SecurityToken;

/**
* @deprecated 2.0..3.0 Use SpellCheckAdminExtension instead
*/
class SpellCheckMiddleware implements HTTPMiddleware
{
use Configurable;
Expand All @@ -25,27 +25,6 @@ class SpellCheckMiddleware implements HTTPMiddleware

public function process(HTTPRequest $request, callable $delegate)
{
// Set settings
$editor = $this->config()->get('editor');

/** @var TinyMCEConfig $editorConfig */
$editorConfig = TinyMCEConfig::get($editor);

$editorConfig->enablePlugins('spellchecker');
$editorConfig->addButtonsToLine(2, 'spellchecker');

$token = SecurityToken::inst();

$editorConfig
->setOption('spellchecker_rpc_url', Director::absoluteURL($token->addToUrl('spellcheck/')))
->setOption('browser_spellcheck', false)
->setOption('spellchecker_languages', implode(',', $this->getLanguages()));

$defaultLocale = $this->getDefaultLocale();
if ($defaultLocale) {
$editorConfig->setOption('spellchecker_language', $defaultLocale);
}

return $delegate($request);
}

Expand Down

0 comments on commit 42d24df

Please sign in to comment.