From d23a91ba6c69fc74276162d8b9ced2e87d813bdf Mon Sep 17 00:00:00 2001 From: yurabakhtin Date: Mon, 27 Sep 2021 11:54:04 +0300 Subject: [PATCH] Text editor --- Events.php | 37 ++++++++++++++ Module.php | 47 ++++++++++++++++++ assets/Assets.php | 41 +++++++++++++++ components/BaseFileController.php | 40 +++++++++++++++ config.php | 18 +++++++ controllers/EditController.php | 51 +++++++++++++++++++ controllers/ViewController.php | 40 +++++++++++++++ filehandler/EditFileHandler.php | 34 +++++++++++++ filehandler/ViewFileHandler.php | 39 +++++++++++++++ models/FileUpdate.php | 80 ++++++++++++++++++++++++++++++ resources/js/humhub.text_editor.js | 25 ++++++++++ views/edit/index.php | 41 +++++++++++++++ views/view/index.php | 34 +++++++++++++ 13 files changed, 527 insertions(+) create mode 100644 Events.php create mode 100644 Module.php create mode 100644 assets/Assets.php create mode 100644 components/BaseFileController.php create mode 100644 config.php create mode 100644 controllers/EditController.php create mode 100644 controllers/ViewController.php create mode 100644 filehandler/EditFileHandler.php create mode 100644 filehandler/ViewFileHandler.php create mode 100644 models/FileUpdate.php create mode 100644 resources/js/humhub.text_editor.js create mode 100644 views/edit/index.php create mode 100644 views/view/index.php diff --git a/Events.php b/Events.php new file mode 100644 index 0000000..3550cce --- /dev/null +++ b/Events.php @@ -0,0 +1,37 @@ +sender; + + /* @var $module Module */ + $module = Yii::$app->getModule('text-editor'); + + if (!$module->isSupportedType($collection->file)) { + return; + } + + if ($collection->type == FileHandlerCollection::TYPE_EDIT && $module->canEdit($collection->file)) { + $collection->register(new filehandler\EditFileHandler()); + } + + if ($collection->type == FileHandlerCollection::TYPE_VIEW && $module->canView($collection->file)) { + $collection->register(new filehandler\ViewFileHandler()); + } + } + +} diff --git a/Module.php b/Module.php new file mode 100644 index 0000000..a1b8bff --- /dev/null +++ b/Module.php @@ -0,0 +1,47 @@ +textExtensions); + } + + public function canEdit(File $file): bool + { + return $this->isSupportedType($file) && + $file->canDelete() && + is_writable($file->getStore()->get()); + } + + public function canView(File $file): bool + { + return $this->isSupportedType($file) && + $file->canRead() && + is_readable($file->getStore()->get()); + } + +} diff --git a/assets/Assets.php b/assets/Assets.php new file mode 100644 index 0000000..d78a1ab --- /dev/null +++ b/assets/Assets.php @@ -0,0 +1,41 @@ + View::POS_END + ]; + + /** + * @inheritdoc + */ + public $publishOptions = [ + 'forceCopy' => true + ]; + +} diff --git a/components/BaseFileController.php b/components/BaseFileController.php new file mode 100644 index 0000000..753f903 --- /dev/null +++ b/components/BaseFileController.php @@ -0,0 +1,40 @@ +request->get('guid', Yii::$app->request->post('guid')); + $file = $class::findOne(['guid' => $guid]); + if (empty($file)) { + throw new HttpException(404, Yii::t('TextEditorModule.base', 'Could not find requested file!')); + } + + return $file; + } + +} diff --git a/config.php b/config.php new file mode 100644 index 0000000..684d769 --- /dev/null +++ b/config.php @@ -0,0 +1,18 @@ + 'text-editor', + 'class' => 'humhub\modules\text_editor\Module', + 'namespace' => 'humhub\modules\text_editor', + 'events' => [ + [FileHandlerCollection::class, FileHandlerCollection::EVENT_INIT, [Events::class, 'onFileHandlerCollection']], + ], +]; diff --git a/controllers/EditController.php b/controllers/EditController.php new file mode 100644 index 0000000..cbaea9e --- /dev/null +++ b/controllers/EditController.php @@ -0,0 +1,51 @@ +getFile(FileUpdate::class); + + if (!$file->canDelete()) { + throw new HttpException(401, Yii::t('TextEditorModule.base', 'Insufficient permissions!')); + } + + if (!is_writable($file->getStore()->get())) { + throw new HttpException(403, Yii::t('TextEditorModule.base', 'File is not writable!')); + } + + if ($file->load(Yii::$app->request->post())) { + if ($file->save()) { + return $this->asJson(['result' => Yii::t('TextEditorModule.base', 'Content of the file :fileName has been updated.', [':fileName' => '"' . $file->file_name . '"'])]); + } else { + return $this->asJson(['error' => Yii::t('TextEditorModule.base', 'File :fileName could not be updated.', [':fileName' => '"' . $file->file_name . '"'])]); + } + } + + return $this->renderAjax('index', [ + 'file' => $file, + ]); + } + +} diff --git a/controllers/ViewController.php b/controllers/ViewController.php new file mode 100644 index 0000000..d0f5f68 --- /dev/null +++ b/controllers/ViewController.php @@ -0,0 +1,40 @@ +getFile(); + + if (!$file->canRead()) { + throw new HttpException(401, Yii::t('TextEditorModule.base', 'Insufficient permissions!')); + } + + if (!is_readable($file->getStore()->get())) { + throw new HttpException(403, Yii::t('TextEditorModule.base', 'File is not readable!')); + } + + return $this->renderAjax('index', [ + 'file' => $file, + ]); + } + +} diff --git a/filehandler/EditFileHandler.php b/filehandler/EditFileHandler.php new file mode 100644 index 0000000..f5133e9 --- /dev/null +++ b/filehandler/EditFileHandler.php @@ -0,0 +1,34 @@ + Yii::t('TextEditorModule.base', 'Edit'), + 'data-action-url' => Url::to(['/text-editor/edit', 'guid' => $this->file->guid]), + 'data-action-click' => 'ui.modal.load', + ]; + } + +} diff --git a/filehandler/ViewFileHandler.php b/filehandler/ViewFileHandler.php new file mode 100644 index 0000000..b2a2498 --- /dev/null +++ b/filehandler/ViewFileHandler.php @@ -0,0 +1,39 @@ + Yii::t('TextEditorModule.base', 'View'), + 'data-action-url' => Url::to(['/text-editor/view', 'guid' => $this->file->guid]), + 'data-action-click' => 'ui.modal.load', + ]; + } + +} diff --git a/models/FileUpdate.php b/models/FileUpdate.php new file mode 100644 index 0000000..f4a127f --- /dev/null +++ b/models/FileUpdate.php @@ -0,0 +1,80 @@ +newFileContent = file_get_contents($this->getStore()->get()); + } + + /** + * @inheritdoc + */ + public function beforeValidate() + { + if ($this->newFileContent && $this->size === null) { + $this->setFileSize(); + } + + return parent::beforeValidate(); + } + + /** + * @inheritdoc + */ + public function rules() + { + $rules = [ + [['newFileContent'], 'safe'], + ]; + + return array_merge(parent::rules(), $rules); + } + + /** + * @inheritdoc + */ + public function afterSave($insert, $changedAttributes) + { + parent::afterSave($insert, $changedAttributes); + $this->store->setContent($this->newFileContent); + } + + /** + * Sets the file size by newFileContent + */ + protected function setFileSize() + { + if (function_exists('mb_strlen')) { + $this->size = mb_strlen($this->newFileContent, '8bit'); + } else { + $this->size = strlen($this->newFileContent); + } + } + +} diff --git a/resources/js/humhub.text_editor.js b/resources/js/humhub.text_editor.js new file mode 100644 index 0000000..909f0c1 --- /dev/null +++ b/resources/js/humhub.text_editor.js @@ -0,0 +1,25 @@ +humhub.module('text_editor', function (module, require, $) { + var client = require('client'); + + var updateContent = function (evt) { + client.submit(evt).then(function (response) { + if (response.result) { + var modal = action.Component.instance(evt.$trigger.closest('.modal')); + if (modal) { + modal.clear(); + modal.close(); + } + module.log.success(response.result); + } else if (response.error) { + module.log.error(response, true); + } + }).catch(function (e) { + module.log.error(e, true); + }); + }; + + module.export({ + updateContent: updateContent, + }); + +}); \ No newline at end of file diff --git a/views/edit/index.php b/views/edit/index.php new file mode 100644 index 0000000..ca24088 --- /dev/null +++ b/views/edit/index.php @@ -0,0 +1,41 @@ + + + Yii::t('TextEditorModule.base', 'Edit file', ['fileName' => Html::encode($file->file_name)]), + 'options' => ['style' => 'width:95%'], + ]) ?> + 'post']) ?> + + + + + + \ No newline at end of file diff --git a/views/view/index.php b/views/view/index.php new file mode 100644 index 0000000..2325c35 --- /dev/null +++ b/views/view/index.php @@ -0,0 +1,34 @@ + + + Yii::t('TextEditorModule.base', 'View file', ['fileName' => Html::encode($file->file_name)]), + 'options' => ['style' => 'width:95%'], + ]) ?> + + + + + \ No newline at end of file