Skip to content

Commit

Permalink
Merge pull request #2718 from nextcloud/enh/config-options
Browse files Browse the repository at this point in the history
Add option to set default file extension and disable rich editing
  • Loading branch information
juliusknorr authored Jul 19, 2022
2 parents 52a82ad + 09c9489 commit 65a7ce8
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 66 deletions.
4 changes: 2 additions & 2 deletions js/editor-rich.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor-rich.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/files-modal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/files-modal.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\Text\Listeners\FilesSharingLoadAdditionalScriptsListener;
use OCA\Text\Listeners\LoadViewerListener;
use OCA\Text\Listeners\RegisterDirectEditorEventListener;
use OCA\Text\Service\ConfigService;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand Down Expand Up @@ -66,9 +67,9 @@ public function register(IRegistrationContext $context): void {
}

public function boot(IBootContext $context): void {
$context->injectFn(function (ITemplateManager $templateManager, IL10N $l) {
$templateManager->registerTemplateFileCreator(function () use ($l) {
$markdownFile = new TemplateFileCreator(Application::APP_NAME, $l->t('New text file'), '.md');
$context->injectFn(function (ITemplateManager $templateManager, IL10N $l, ConfigService $configService) {
$templateManager->registerTemplateFileCreator(function () use ($l, $configService) {
$markdownFile = new TemplateFileCreator(Application::APP_NAME, $l->t('New text file'), '.' . $configService->getDefaultFileExtension());
$markdownFile->addMimetype('text/markdown');
$markdownFile->addMimetype('text/plain');
$markdownFile->setIconClass('icon-filetype-text');
Expand Down
29 changes: 5 additions & 24 deletions lib/Listeners/FilesLoadAdditionalScriptsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,18 @@
namespace OCA\Text\Listeners;

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Text\AppInfo\Application;
use OCA\Text\Service\InitialStateProvider;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IUserSession;

/**
* @implements IEventListener<Event>
*/
class FilesLoadAdditionalScriptsListener implements IEventListener {
/** @var IConfig */
protected $config;
/** @var IInitialStateService */
protected $initialStateService;
/** @var IUserSession */
protected $userSession;
private InitialStateProvider $initialStateProvider;

public function __construct(IConfig $config, IInitialStateService $initialStateService, IUserSession $userSession) {
$this->config = $config;
$this->initialStateService = $initialStateService;
$this->userSession = $userSession;
public function __construct(InitialStateProvider $initialStateProvider) {
$this->initialStateProvider = $initialStateProvider;
}

public function handle(Event $event): void {
Expand All @@ -57,15 +47,6 @@ public function handle(Event $event): void {

\OCP\Util::addScript('text', 'text-files');

$this->initialStateService->provideInitialState(
Application::APP_NAME,
'workspace_available',
$this->config->getAppValue(Application::APP_NAME, 'workspace_available', '1') === '1'
);
$this->initialStateService->provideInitialState(
Application::APP_NAME,
'workspace_enabled',
$this->config->getUserValue($this->userSession->getUser()->getUID(), Application::APP_NAME, 'workspace_enabled', '1') === '1'
);
$this->initialStateProvider->provideState();
}
}
22 changes: 7 additions & 15 deletions lib/Listeners/FilesSharingLoadAdditionalScriptsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,23 @@

namespace OCA\Text\Listeners;

use OCA\Text\AppInfo\Application;
use OCA\Text\Service\InitialStateProvider;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\Util;

/** @implements IEventListener<Event> */
class FilesSharingLoadAdditionalScriptsListener implements IEventListener {
/** @var IConfig */
protected $config;
/** @var IInitialStateService */
protected $initialStateService;
protected InitialStateProvider $initialStateProvider;

public function __construct(IConfig $config, IInitialStateService $initialStateService) {
$this->config = $config;
$this->initialStateService = $initialStateService;
public function __construct(IConfig $config, InitialStateProvider $initialStateProvider) {
$this->initialStateProvider = $initialStateProvider;
}

public function handle(Event $event): void {
\OCP\Util::addScript('text', 'text-public');
Util::addScript('text', 'text-public');

$this->initialStateService->provideInitialState(
Application::APP_NAME,
'workspace_available',
$this->config->getAppValue(Application::APP_NAME, 'workspace_available', '1') === '1'
);
$this->initialStateProvider->provideState();
}
}
12 changes: 11 additions & 1 deletion lib/Listeners/LoadViewerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,26 @@

namespace OCA\Text\Listeners;

use OCA\Text\Service\InitialStateProvider;
use OCA\Viewer\Event\LoadViewer;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

/** @implements IEventListener<Event> */
class LoadViewerListener implements IEventListener {
private InitialStateProvider $initialStateProvider;

public function __construct(InitialStateProvider $initialStateProvider) {
$this->initialStateProvider = $initialStateProvider;
}

public function handle(Event $event): void {
if (!$event instanceof LoadViewer) {
return;
}
\OCP\Util::addScript('text', 'text-viewer', 'viewer');
Util::addScript('text', 'text-viewer', 'viewer');

$this->initialStateProvider->provideState();
}
}
33 changes: 33 additions & 0 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace OCA\Text\Service;

use OCA\Text\AppInfo\Application;
use OCP\IConfig;

class ConfigService {
private IConfig $config;

public function __construct(IConfig $config) {
$this->config = $config;
}

public function getDefaultFileExtension(): string {
return $this->config->getAppValue(Application::APP_NAME, 'default_file_extension', 'md');
}

public function isRichEditingEnabled(): bool {
return ($this->config->getAppValue(Application::APP_NAME, 'rich_editing_enabled', '1') === '1');
}

public function isRichWorkspaceAvailable(): bool {
return $this->config->getAppValue(Application::APP_NAME, 'workspace_available', '1') === '1';
}

public function isRichWorkspaceEnabledForUser(?string $userId): bool {
if ($userId === null) {
return true;
}
return $this->config->getUserValue($userId, Application::APP_NAME, 'workspace_enabled', '1') === '1';
}
}
39 changes: 39 additions & 0 deletions lib/Service/InitialStateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace OCA\Text\Service;

use OCP\AppFramework\Services\IInitialState;

class InitialStateProvider {
private IInitialState $initialState;
private ConfigService $configService;
private ?string $userId;

public function __construct(IInitialState $initialState, ConfigService $configService, ?string $userId) {
$this->initialState = $initialState;
$this->configService = $configService;
$this->userId = $userId;
}

public function provideState(): void {
$this->initialState->provideInitialState(
'workspace_available',
$this->configService->isRichWorkspaceAvailable()
);

$this->initialState->provideInitialState(
'workspace_enabled',
$this->configService->isRichWorkspaceEnabledForUser($this->userId)
);

$this->initialState->provideInitialState(
'default_file_extension',
$this->configService->getDefaultFileExtension()
);

$this->initialState->provideInitialState(
'rich_editing_enabled',
$this->configService->isRichEditingEnabled()
);
}
}
3 changes: 2 additions & 1 deletion src/components/EditorWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import { EditorContent } from '@tiptap/vue-2'
import { getVersion, receiveTransaction } from 'prosemirror-collab'
import { Step } from 'prosemirror-transform'
import { getCurrentUser } from '@nextcloud/auth'
import { loadState } from '@nextcloud/initial-state'

import {
EDITOR,
Expand Down Expand Up @@ -299,7 +300,7 @@ export default {
return this.isDirectEditing || (document.getElementById('isPublic') && document.getElementById('isPublic').value === '1')
},
isRichEditor() {
return this.mime === 'text/markdown'
return loadState('text', 'rich_editing_enabled', true) && this.mime === 'text/markdown'
},
fileExtension() {
return this.relativePath ? this.relativePath.split('/').pop().split('.').pop() : 'txt'
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*
*/

import { loadState } from '@nextcloud/initial-state'
import { openMimetypes } from './mime.js'
import RichWorkspace from '../views/RichWorkspace.vue'
import { imagePath } from '@nextcloud/router'
Expand Down Expand Up @@ -56,7 +57,7 @@ const registerFileCreate = () => {
menu.addMenuEntry({
id: 'file',
displayName: t('text', 'New text file'),
templateName: t('text', 'New text file') + '.md',
templateName: t('text', 'New text file') + '.' + loadState('text', 'default_file_extension'),
iconClass: 'icon-filetype-text',
fileType: 'file',
actionHandler(name) {
Expand Down

0 comments on commit 65a7ce8

Please sign in to comment.