diff --git a/composer/composer/autoload_classmap.php b/composer/composer/autoload_classmap.php index dd4f029eb45..1c2a562a99b 100644 --- a/composer/composer/autoload_classmap.php +++ b/composer/composer/autoload_classmap.php @@ -46,6 +46,7 @@ 'OCA\\Text\\Listeners\\LoadViewerListener' => $baseDir . '/../lib/Listeners/LoadViewerListener.php', 'OCA\\Text\\Listeners\\NodeCopiedListener' => $baseDir . '/../lib/Listeners/NodeCopiedListener.php', 'OCA\\Text\\Listeners\\RegisterDirectEditorEventListener' => $baseDir . '/../lib/Listeners/RegisterDirectEditorEventListener.php', + 'OCA\\Text\\Listeners\\RegisterTemplateCreatorListener' => $baseDir . '/../lib/Listeners/RegisterTemplateCreatorListener.php', 'OCA\\Text\\Middleware\\Attribute\\RequireDocumentBaseVersionEtag' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentBaseVersionEtag.php', 'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSession' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentSession.php', 'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSessionOrUserOrShareToken' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentSessionOrUserOrShareToken.php', diff --git a/composer/composer/autoload_static.php b/composer/composer/autoload_static.php index aac06275a14..516ca76649b 100644 --- a/composer/composer/autoload_static.php +++ b/composer/composer/autoload_static.php @@ -61,6 +61,7 @@ class ComposerStaticInitText 'OCA\\Text\\Listeners\\LoadViewerListener' => __DIR__ . '/..' . '/../lib/Listeners/LoadViewerListener.php', 'OCA\\Text\\Listeners\\NodeCopiedListener' => __DIR__ . '/..' . '/../lib/Listeners/NodeCopiedListener.php', 'OCA\\Text\\Listeners\\RegisterDirectEditorEventListener' => __DIR__ . '/..' . '/../lib/Listeners/RegisterDirectEditorEventListener.php', + 'OCA\\Text\\Listeners\\RegisterTemplateCreatorListener' => __DIR__ . '/..' . '/../lib/Listeners/RegisterTemplateCreatorListener.php', 'OCA\\Text\\Middleware\\Attribute\\RequireDocumentBaseVersionEtag' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentBaseVersionEtag.php', 'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSession' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentSession.php', 'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSessionOrUserOrShareToken' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentSessionOrUserOrShareToken.php', diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index e8e7c83e2f8..31b37a8aa0e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -39,12 +39,11 @@ use OCA\Text\Listeners\LoadViewerListener; use OCA\Text\Listeners\NodeCopiedListener; use OCA\Text\Listeners\RegisterDirectEditorEventListener; +use OCA\Text\Listeners\RegisterTemplateCreatorListener; use OCA\Text\Middleware\SessionMiddleware; use OCA\Text\Notification\Notifier; -use OCA\Text\Service\ConfigService; use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent; use OCA\Viewer\Event\LoadViewer; -use OCP\App\IAppManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; @@ -55,9 +54,7 @@ use OCP\Files\Events\Node\BeforeNodeRenamedEvent; use OCP\Files\Events\Node\BeforeNodeWrittenEvent; use OCP\Files\Events\Node\NodeCopiedEvent; -use OCP\Files\Template\ITemplateManager; -use OCP\Files\Template\TemplateFileCreator; -use OCP\IL10N; +use OCP\Files\Template\RegisterTemplateCreatorEvent; class Application extends App implements IBootstrap { public const APP_NAME = 'text'; @@ -79,23 +76,12 @@ public function register(IRegistrationContext $context): void { $context->registerEventListener(BeforeNodeDeletedEvent::class, BeforeNodeDeletedListener::class); $context->registerEventListener(AddMissingIndicesEvent::class, AddMissingIndicesListener::class); $context->registerEventListener(BeforeAssistantNotificationEvent::class, BeforeAssistantNotificationListener::class); + $context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class); $context->registerNotifierService(Notifier::class); $context->registerMiddleware(SessionMiddleware::class); } public function boot(IBootContext $context): void { - $context->injectFn(function (ITemplateManager $templateManager, IL10N $l, ConfigService $configService, IAppManager $appManager) { - $templateManager->registerTemplateFileCreator(function () use ($l, $configService, $appManager) { - $markdownFile = new TemplateFileCreator(Application::APP_NAME, $l->t('New text file'), '.' . $configService->getDefaultFileExtension()); - $markdownFile->addMimetype('text/markdown'); - $markdownFile->addMimetype('text/plain'); - $markdownFile->setIconSvgInline(file_get_contents($appManager->getAppPath('text') . '/img/article.svg')); - $markdownFile->setRatio(1); - $markdownFile->setOrder(10); - $markdownFile->setActionLabel($l->t('Create new text file')); - return $markdownFile; - }); - }); } } diff --git a/lib/Listeners/RegisterTemplateCreatorListener.php b/lib/Listeners/RegisterTemplateCreatorListener.php new file mode 100644 index 00000000000..63e20f23b1c --- /dev/null +++ b/lib/Listeners/RegisterTemplateCreatorListener.php @@ -0,0 +1,64 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Text\Listeners; + +use OCA\Text\AppInfo\Application; +use OCA\Text\Service\ConfigService; +use OCP\App\IAppManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Template\RegisterTemplateCreatorEvent; +use OCP\Files\Template\TemplateFileCreator; +use OCP\IL10N; + +/** @implements IEventListener */ +class RegisterTemplateCreatorListener implements IEventListener { + public function __construct( + private IL10N $l10n, + private ConfigService $configService, + private IAppManager $appManager, + ) { + } + + public function handle(Event $event): void { + if (!$event instanceof RegisterTemplateCreatorEvent) { + return; + } + + $event->getTemplateManager()->registerTemplateFileCreator(function () { + $markdownFile = new TemplateFileCreator(Application::APP_NAME, $this->l10n->t('New text file'), '.' . $this->configService->getDefaultFileExtension()); + $markdownFile->addMimetype('text/markdown'); + $markdownFile->addMimetype('text/plain'); + $markdownFile->setIconSvgInline(file_get_contents($this->appManager->getAppPath('text') . '/img/article.svg')); + $markdownFile->setRatio(1); + $markdownFile->setOrder(10); + $markdownFile->setActionLabel($this->l10n->t('Create new text file')); + return $markdownFile; + }); + + + } +}