diff --git a/Plugin.php b/Plugin.php index d64a5a9..0191c5d 100644 --- a/Plugin.php +++ b/Plugin.php @@ -1,4 +1,6 @@ -addCss('assets/contenteditor.css'); $this->addJs('assets/content-tools.min.js'); $this->addJs('assets/contenteditor.js'); + + // Add scripts only once + $this->renderPartial('@scripts.htm'); } } @@ -125,7 +131,7 @@ public function getFile() { if (Content::load($this->getTheme(), $this->file)) { return $this->renderContent($this->file); - } else if (Content::load($this->getTheme(), $this->defaultFile)) { // if no locale file exists -> render the default, without language suffix + } elseif (Content::load($this->getTheme(), $this->defaultFile)) { // if no locale file exists -> render the default, without language suffix return $this->renderContent($this->defaultFile); } @@ -144,17 +150,17 @@ public function setFile($file) public function setTranslateFile($file) { - $translate = \RainLab\Translate\Classes\Translator::instance(); + $translate = Translator::instance(); $defaultLocale = $translate->getDefaultLocale(); $locale = $translate->getLocale(); // Compability with Rainlab.StaticPage // StaticPages content does not append default locale to file. if ($this->fileExists($file) && $locale === $defaultLocale) { - return $file; + return $file; } - return substr_replace($file, '.'.$locale, strrpos($file, '.'), 0); + return substr_replace($file, '.' . $locale, strrpos($file, '.'), 0); } public function checkEditor() @@ -163,12 +169,13 @@ public function checkEditor() return $backendUser && $backendUser->hasAccess('samuell.contenteditor.editor'); } - public function fileExists($file) { + public function fileExists($file) + { return File::exists((new Content)->getFilePath($file)); } public function translateExists() { - return class_exists('\RainLab\Translate\Classes\Translator'); + return class_exists(Translator::class); } } diff --git a/components/contenteditor/default.htm b/components/contenteditor/default.htm index e87f8be..a3feadc 100644 --- a/components/contenteditor/default.htm +++ b/components/contenteditor/default.htm @@ -6,32 +6,3 @@ {% if __SELF__.class %}class="{{ __SELF__.class }}"{% endif %}> {{ __SELF__.content|raw }} - -{% if __SELF__.renderCount == 1 %} - {% put scripts %} - - {% endput %} - {% put styles %} - - {% endput %} -{% endif %} diff --git a/components/contenteditor/render.htm b/components/contenteditor/render.htm index 4b46174..a6ee2d9 100644 --- a/components/contenteditor/render.htm +++ b/components/contenteditor/render.htm @@ -1,4 +1,3 @@ - {% if __SELF__.fixture %}<{{ __SELF__.fixture }} {% if __SELF__.class %}class="{{ __SELF__.class }}"{% endif %}>{% endif %} {{ content|raw }} {% if __SELF__.fixture %}{% endif %} diff --git a/components/contenteditor/scripts.htm b/components/contenteditor/scripts.htm new file mode 100644 index 0000000..087dff8 --- /dev/null +++ b/components/contenteditor/scripts.htm @@ -0,0 +1,26 @@ +{% put scripts %} + +{% endput %} +{% put styles %} + +{% endput %} diff --git a/http/controllers/AdditionalStylesController.php b/http/controllers/AdditionalStylesController.php index c5dc5c1..4c0aaa8 100644 --- a/http/controllers/AdditionalStylesController.php +++ b/http/controllers/AdditionalStylesController.php @@ -1,4 +1,6 @@ -getClientOriginalExtension()); - $fileName = File::name($fileName).'.'.$extension; + $fileName = File::name($fileName) . '.' . $extension; /* * File name contains non-latin characters, attempt to slug the value @@ -54,29 +58,28 @@ public function upload() $path = MediaLibrary::validatePath($path); $realPath = empty(trim($uploadedFile->getRealPath())) - ? $uploadedFile->getPath() . DIRECTORY_SEPARATOR . $uploadedFile->getFileName() - : $uploadedFile->getRealPath(); + ? $uploadedFile->getPath() . DIRECTORY_SEPARATOR . $uploadedFile->getFileName() + : $uploadedFile->getRealPath(); MediaLibrary::instance()->put( - $path.'/'.$fileName, + $path . '/' . $fileName, File::get($realPath) ); list($width, $height) = getimagesize($uploadedFile); return Response::json([ - 'url' => MediaLibrary::instance()->getPathUrl($path.'/'.$fileName), - 'filePath' => $path.'/'.$fileName, + 'url' => MediaLibrary::instance()->getPathUrl($path . '/' . $fileName), + 'filePath' => $path . '/' . $fileName, 'filename' => $fileName, 'size' => [ - $width, - $height - ] + $width, + $height + ] ]); } catch (Exception $ex) { throw new ApplicationException($ex); } - } public function save() @@ -86,14 +89,14 @@ public function save() $width = post('width'); $height = post('height'); $filePath = post('filePath'); - $relativeFilePath = config('cms.storage.media.path').$filePath; + $relativeFilePath = config('cms.storage.media.path', config('system.storage.media.path')) . $filePath; if ($crop && $crop != '0,0,1,1') { $crop = explode(',', $crop); $file = MediaLibrary::instance()->get(post('filePath')); - $tempDirectory = temp_path().'/contenteditor'; - $tempFilePath = temp_path().post('filePath'); + $tempDirectory = temp_path() . '/contenteditor'; + $tempFilePath = temp_path() . post('filePath'); File::makeDirectory($tempDirectory, 0777, true, true); if (!File::put($tempFilePath, $file)) { diff --git a/models/Settings.php b/models/Settings.php index 8307542..775254c 100644 --- a/models/Settings.php +++ b/models/Settings.php @@ -1,7 +1,8 @@ -additional_styles = File::get(plugins_path() . '/samuell/contenteditor/assets/additional-css.css'); - $this->enabled_buttons = ["bold", "italic", "link", "align-left", "align-center", "align-right", "heading", "subheading", "subheading3", "subheading4", "subheading5", "paragraph", "unordered-list", "ordered-list", "table", "indent", "unindent", "line-break", "image", "video", "preformatted"]; + $this->enabled_buttons = ['bold', 'italic', 'link', 'align-left', 'align-center', 'align-right', 'heading', 'subheading', 'subheading3', 'subheading4', 'subheading5', 'paragraph', 'unordered-list', 'ordered-list', 'table', 'indent', 'unindent', 'line-break', 'image', 'video', 'preformatted']; } // list of buttons @@ -91,12 +92,14 @@ public static function renderCss() if (Cache::has(self::CACHE_KEY)) { return Cache::get(self::CACHE_KEY); } + try { $customCss = self::compileCss(); Cache::forever(self::CACHE_KEY, $customCss); } catch (Exception $ex) { $customCss = '/* ' . $ex->getMessage() . ' */'; } + return $customCss; } diff --git a/routes.php b/routes.php index 5033f46..3ba13a4 100644 --- a/routes.php +++ b/routes.php @@ -1,14 +1,17 @@ 'contenteditor'], function () { - Route::middleware(['web', EditorPermissionsMiddleware::class])->group(function () { - Route::post('image/upload', 'Samuell\ContentEditor\Http\Controllers\ImageController@upload'); - Route::post('image/save', 'Samuell\ContentEditor\Http\Controllers\ImageController@save'); - }); + Route::middleware(['web', EditorPermissionsMiddleware::class]) + ->group(function () { + Route::post('image/upload', [ImageController::class, 'upload']); + Route::post('image/save', [ImageController::class, 'save']); + }); // Additional styles route - Route::get('styles', 'Samuell\ContentEditor\Http\Controllers\AdditionalStylesController@render'); + Route::get('styles', [AdditionalStylesController::class, 'render']); }); diff --git a/updates/version.yaml b/updates/version.yaml index ecf04aa..afe8626 100644 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -55,3 +55,5 @@ 1.3.3: - Allow only image extensions for upload - Fix italic tag name +1.3.4: + - Small fixes & code cleanup