From 6ef883c69495eeedb5d65e7860087772b583ad22 Mon Sep 17 00:00:00 2001 From: "James (DPC)" Date: Fri, 9 Apr 2021 11:59:23 +1000 Subject: [PATCH] Set version as a property, define a local SVG source if none specified in configuration --- client/static/svg/icons.2.23.0.svg | 1 + composer.json | 7 +++++- src/Fields/TrumboywgEditorField.php | 37 +++++++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 client/static/svg/icons.2.23.0.svg diff --git a/client/static/svg/icons.2.23.0.svg b/client/static/svg/icons.2.23.0.svg new file mode 100644 index 0000000..73a7986 --- /dev/null +++ b/client/static/svg/icons.2.23.0.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/composer.json b/composer.json index f8f6dde..6632be8 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,12 @@ "tests/" ] } - }, + }, + "extra" : { + "expose" : [ + "client/static" + ] + }, "require": { "silverstripe/framework" : "^4", "ezyang/htmlpurifier" : "^4.13" diff --git a/src/Fields/TrumboywgEditorField.php b/src/Fields/TrumboywgEditorField.php index 7628745..f4eded5 100644 --- a/src/Fields/TrumboywgEditorField.php +++ b/src/Fields/TrumboywgEditorField.php @@ -3,6 +3,9 @@ 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; @@ -18,6 +21,12 @@ class TrumboywgEditorField extends TextareaField { 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 @@ -44,7 +53,31 @@ protected function getFieldOptions() { ] ]; } + + // 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; } @@ -87,7 +120,7 @@ public function Field($properties = []) { ); } Requirements::javascript( - "https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/2.23.0/trumbowyg.min.js", + "https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/{$this->version}/trumbowyg.min.js", [ "integrity" => "sha512-sffB9/tXFFTwradcJHhojkhmrCj0hWeaz8M05Aaap5/vlYBfLx5Y7woKi6y0NrqVNgben6OIANTGGlojPTQGEw==", "crossorigin" => "anonymous" @@ -102,7 +135,7 @@ public function Field($properties = []) { "trumbowyg_editor" ); Requirements::css( - "https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/2.23.0/ui/trumbowyg.min.css", + "https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/{$this->version}/ui/trumbowyg.min.css", "screen", [ "integrity" => "sha512-iw/TO6rC/bRmSOiXlanoUCVdNrnJBCOufp2s3vhTPyP1Z0CtTSBNbEd5wIo8VJanpONGJSyPOZ5ZRjZ/ojmc7g==",