Skip to content

Commit

Permalink
Merge pull request #2 from nswdpc/feat-improvements
Browse files Browse the repository at this point in the history
General module improvements
  • Loading branch information
tardinha authored Oct 21, 2021
2 parents 95af2db + e9e6bd0 commit 2f1434d
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 201 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Per [composer.json](/composer.json):

The field pulls in required Trumbowyg JS and CSS assets from [cdnjs.com](https://cdnjs.com) along with their respective Sub Resource Integrity (SRI) hashes.

If you wish to use your own jQuery, set the `TrumboywgEditorField.use_own_jquery` configuration value to `true` in your project configuration.
If you wish to use your own jQuery, set the `TrumboywgEditorField.use_own_jquery` configuration value to `false` in your project configuration. When false, the module will not include its own jQuery.

## Installation

Expand Down
4 changes: 3 additions & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
Name: nswdpc-trumbowyg-editor
---
NSWDPC\Utilities\Trumbowyg\TrumboywgEditorField:
NSWDPC\Utilities\Trumbowyg\TrumbowygEditorField:
# warning: prior to modifying these, read the README.md
editor_options:
semantic: true
Expand All @@ -14,6 +14,7 @@ NSWDPC\Utilities\Trumbowyg\TrumboywgEditorField:
- redo
-
- p
- h2
- h3
- h4
- h5
Expand Down Expand Up @@ -69,6 +70,7 @@ NSWDPC\Utilities\Trumbowyg\TrumboywgEditorField:
- strong
- em
- br
- h2
- h3
- h4
- h5
Expand Down
136 changes: 136 additions & 0 deletions src/Fields/TrumbowygEditorField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace NSWDPC\Utilities\Trumbowyg;

use SilverStripe\Core\Convert;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use Exception;
use DOMDocument;


class TrumbowygEditorField extends TextareaField {

private static $casting = [
'Value' => 'HTMLText',
];

private static $include_own_jquery = true;

/**
* Get field options
* @return array
*/
protected function getFieldOptions() {
$options = $this->config()->get('editor_options');
if( empty($options) || !is_array($options) ) {
// Fallback options in case of none configured
$options = [
"semantic" => true,
"removeformatPasted" => true,
"resetCss" => true,
"autogrow" => true,
"btns" => [
[ "undo", "redo" ],
[ "p", "h2","h3", "h4", "h5", "strong", "em" ],
[ "link", "" ],
[ "unorderedList", "orderedList" ],
[ "removeformat" ],
[ "fullscreen" ]
],
"tagsToKeep" => [
"p",
"i","b", "strong", "em", "br",
"h2","h3","h4","h5","h6",
"ol","ul","li","a"
]
];
}
$options['tagsToRemove'] = self::getDeniedTags();
return $options;
}

/**
* These tags are denied by default
* @return array
*/
public static function getDeniedTags() {
return [
'form',
'script',
'link',
'style',
'body',
'html',
'head',
'meta',
'applet',
'object',
'iframe',
'img',
'picture',
'video',
];
}

/**
* Returns the field
*/
public function Field($properties = []) {
$this->setAttribute('data-tw','1');

if($this->config()->get('include_own_jquery')) {
Requirements::javascript(
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js",
[
"integrity" => "sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==",
"crossorigin" => "anonymous"
]
);
}
Requirements::javascript(
"https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/2.25.1/trumbowyg.min.js",
[
"integrity" => "sha512-t4CFex/T+ioTF5y0QZnCY9r5fkE8bMf9uoNH2HNSwsiTaMQMO0C9KbKPMvwWNdVaEO51nDL3pAzg4ydjWXaqbg==",
"crossorigin" => "anonymous"
]
);
// import template with options
$custom_script = ArrayData::create([
'ID' => $this->ID(),
'Options' => json_encode( $this->getFieldOptions() )
])->renderWith('NSWDPC/Utilities/Trumbowyg/Script');
Requirements::customScript(
$custom_script,
"trumbowyg_editor_" . $this->ID()
);
Requirements::css(
"https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/2.25.1/ui/trumbowyg.min.css",
"screen",
[
"integrity" => "sha512-nwpMzLYxfwDnu68Rt9PqLqgVtHkIJxEPrlu3PfTfLQKVgBAlTKDmim1JvCGNyNRtyvCx1nNIVBfYm8UZotWd4Q==",
"crossorigin" => "anonymous"
]
);
return parent::Field($properties);
}

/**
* Return the value, sanitised
*/
public function Value() {
return $this->dataValue();
}

/**
* Return cleaned data value
*/
public function dataValue() {
$sanitiser = new ContentSanitiser();
$this->value = $sanitiser->clean($this->value);
return $this->value;
}

}
160 changes: 9 additions & 151 deletions src/Fields/TrumboywgEditorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,163 +2,21 @@

namespace NSWDPC\Utilities\Trumbowyg;

use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Core\Manifest\ResourceURLGenerator;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use Exception;
use SilverStripe\Dev\Deprecation;


class TrumboywgEditorField extends TextareaField {

private static $casting = [
'Value' => 'HTMLText',
];

private static $include_own_jquery = true;

/**
* @var string
* Chore: when updating the version, ensure the SRI hashes are updated
*/
protected $version = "2.23.0";

/**
* Get field options
* @return array
*/
protected function getFieldOptions() {
$options = $this->config()->get('editor_options');
if( empty($options) || !is_array($options) ) {
// Fallback options in case of none configured
$options = [
"semantic" => true,
"removeformatPasted" => true,
"resetCss" => true,
"autogrow" => true,
"buttons" => [
[ "undo", "redo" ],
[ "p","h3", "h4", "h5", "strong", "em" ],
[ "link", "" ],
[ "unorderedList", "orderedList" ],
[ "removeformat" ],
[ "fullscreen" ]
],
"tagsToKeep" => [
"p"
]
];
}

// ensure tagsToRemove is set by us
$options['tagsToRemove'] = self::getDeniedTags();

// if no svgPath defined in configuration, set one up
if(empty($options['svgPath'])) {
$svgPath = '';

if($sprite = ModuleResourceLoader::singleton()
->resolvePath(
"nswdpc/silverstripe-trumbowyg:client/static/svg/icons.{$this->version}.svg"
)
) {
// get the path without the ?m= nonce
$svgPath = Injector::inst()->get(ResourceURLGenerator::class)
->setNonceStyle(null)
->urlForResource( $sprite );
}
// load it up, if found
if($svgPath) {
$options['svgPath'] = $svgPath;
$options['svgAbsoluteUsePath'] = false;
}
}

return $options;
}
/**
* This class is provided for backwards compatibility, as the original
* field was created with the incorrect spelling
* Update your code to use `TrumbowygEditorField`
*/
class TrumboywgEditorField extends TrumbowygEditorField {

/**
* These tags are denied by default
* @return array
*/
public static function getDeniedTags() {
return [
'form',
'script',
'link',
'style',
'body',
'html',
'head',
'meta',
'applet',
'object',
'iframe',
'img',
'picture',
'video',
];
}

/**
* Returns the field
* Handle deprecation notice for incorrectly named field
*/
public function Field($properties = []) {
$this->setAttribute('data-tw','1');

if($this->config()->get('include_own_jquery')) {
Requirements::javascript(
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js",
[
"integrity" => "sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==",
"crossorigin" => "anonymous"
]
);
}
Requirements::javascript(
"https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/{$this->version}/trumbowyg.min.js",
[
"integrity" => "sha512-sffB9/tXFFTwradcJHhojkhmrCj0hWeaz8M05Aaap5/vlYBfLx5Y7woKi6y0NrqVNgben6OIANTGGlojPTQGEw==",
"crossorigin" => "anonymous"
]
);
// import template with options
$custom_script = ArrayData::create([
'Options' => json_encode( $this->getFieldOptions(), JSON_UNESCAPED_SLASHES )
])->renderWith('NSWDPC/Utilities/Trumbowyg/Script');
Requirements::customScript(
$custom_script,
"trumbowyg_editor"
);
Requirements::css(
"https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/{$this->version}/ui/trumbowyg.min.css",
"screen",
[
"integrity" => "sha512-iw/TO6rC/bRmSOiXlanoUCVdNrnJBCOufp2s3vhTPyP1Z0CtTSBNbEd5wIo8VJanpONGJSyPOZ5ZRjZ/ojmc7g==",
"crossorigin" => "anonymous"
]
);
Deprecation::notice('1.0', 'TrumboywgEditorField will be removed in 1.0');
return parent::Field($properties);
}

/**
* Return the value, sanitised
*/
public function Value() {
return $this->dataValue();
}

/**
* Return cleaned data value
*/
public function dataValue() {
$sanitiser = new ContentSanitiser();
$this->value = $sanitiser->clean($this->value);
return $this->value;
}

}
Loading

0 comments on commit 2f1434d

Please sign in to comment.