add setting for default file suffix

This commit is contained in:
korelstar 2018-07-24 21:34:05 +02:00 committed by GitHub
parent e11d328597
commit fe52c71e72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 20 deletions

View File

@ -36,6 +36,6 @@ class SettingsController extends Controller
* @NoAdminRequired * @NoAdminRequired
*/ */
public function get() { public function get() {
return new JSONResponse($this->service->get()); return new JSONResponse($this->service->getAll());
} }
} }

View File

@ -86,6 +86,10 @@
opacity: 1 !important; opacity: 1 !important;
} }
#app-settings-content > div + div {
padding-top: 2ex;
}
.note-search span { .note-search span {
background-position: 0 center; background-position: 0 center;
background-origin: content-box; background-origin: content-box;

View File

@ -2,6 +2,8 @@ app.controller('NotesSettingsController',
function($scope, Restangular, $document) { function($scope, Restangular, $document) {
'use strict'; 'use strict';
$scope.extensions = ['.txt', '.md'];
Restangular.one('settings').get().then(function(settings) { Restangular.one('settings').get().then(function(settings) {
if(angular.isObject(settings)) { if(angular.isObject(settings)) {
$scope.settings = settings; $scope.settings = settings;
@ -17,4 +19,8 @@ app.controller('NotesSettingsController',
window.location.reload(true); window.location.reload(true);
}); });
}); });
$document.on('change', '#fileSuffix', function() {
$scope.settings.put();
});
}); });

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -132,7 +132,7 @@ class NotesService {
// check new note exists already and we need to number it // check new note exists already and we need to number it
// pass -1 because no file has id -1 and that will ensure // pass -1 because no file has id -1 and that will ensure
// to only return filenames that dont yet exist // to only return filenames that dont yet exist
$path = $this->generateFileName($folder, $title, "txt", -1); $path = $this->generateFileName($folder, $title, $this->settings->get('fileSuffix'), -1);
$file = $folder->newFile($path); $file = $folder->newFile($path);
return $this->getNote($file, $folder); return $this->getNote($file, $folder);
@ -160,7 +160,7 @@ class NotesService {
// this can fail if access rights are not sufficient or category name is illegal // this can fail if access rights are not sufficient or category name is illegal
try { try {
$currentFilePath = $this->root->getFullPath($file->getPath()); $currentFilePath = $this->root->getFullPath($file->getPath());
$fileExtension = pathinfo($file->getName(), PATHINFO_EXTENSION); $fileSuffix = '.' . pathinfo($file->getName(), PATHINFO_EXTENSION);
// detect (new) folder path based on category name // detect (new) folder path based on category name
if($category===null) { if($category===null) {
@ -178,7 +178,7 @@ class NotesService {
} }
// assemble new file path // assemble new file path
$newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileExtension, $id); $newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileSuffix, $id);
// if the current path is not the new path, the file has to be renamed // if the current path is not the new path, the file has to be renamed
if($currentFilePath !== $newFilePath) { if($currentFilePath !== $newFilePath) {
@ -344,15 +344,15 @@ class NotesService {
* *
* @param Folder $folder a folder to the notes directory * @param Folder $folder a folder to the notes directory
* @param string $title the filename which should be used * @param string $title the filename which should be used
* @param string $extension the extension which should be used * @param string $suffix the suffix (incl. dot) which should be used
* @param int $id the id of the note for which the title should be generated * @param int $id the id of the note for which the title should be generated
* used to see if the file itself has the title and not a different file for * used to see if the file itself has the title and not a different file for
* checking for filename collisions * checking for filename collisions
* @return string the resolved filename to prevent overwriting different * @return string the resolved filename to prevent overwriting different
* files with the same title * files with the same title
*/ */
private function generateFileName (Folder $folder, $title, $extension, $id) { private function generateFileName (Folder $folder, $title, $suffix, $id) {
$path = $title . '.' . $extension; $path = $title . $suffix;
// if file does not exist, that name has not been taken. Similar we don't // if file does not exist, that name has not been taken. Similar we don't
// need to handle file collisions if it is the filename did not change // need to handle file collisions if it is the filename did not change
@ -368,7 +368,7 @@ class NotesService {
} else { } else {
$newTitle = $title . ' (2)'; $newTitle = $title . ' (2)';
} }
return $this->generateFileName($folder, $newTitle, $extension, $id); return $this->generateFileName($folder, $newTitle, $suffix, $id);
} }
} }

View File

@ -17,8 +17,9 @@ class SettingsService
private $root; private $root;
/* Default values */ /* Default values */
private $settings = [ private $defaults = [
"notesPath" => "Notes", "notesPath" => "Notes",
"fileSuffix" => ".txt",
]; ];
public function __construct( public function __construct(
@ -33,16 +34,42 @@ class SettingsService
* @throws \OCP\PreConditionNotMetException * @throws \OCP\PreConditionNotMetException
*/ */
public function set($settings) { public function set($settings) {
foreach($this->settings as $name => $value) { // remove illegal, empty and default settings
$this->settings[$name] = isset($settings[$name]) ? $settings[$name] : $value; foreach($settings as $name => $value) {
if(!array_key_exists($name, $this->defaults)
|| empty($value)
|| $value === $this->defaults[$name]
) {
unset($settings[$name]);
}
} }
$this->config->setUserValue($this->uid, 'notes', 'settings', json_encode($settings));
$this->config->setUserValue($this->uid, 'notes', 'settings', json_encode($this->settings));
} }
public function get($name = null, $default = null) { public function getAll() {
$settings = json_decode($this->config->getUserValue($this->uid, 'notes', 'settings')); $settings = json_decode($this->config->getUserValue($this->uid, 'notes', 'settings'));
if(!$settings || !is_object($settings)) $settings = $this->settings; if(is_object($settings)) {
return $name ? (property_exists($settings, $name) ? $settings->{$name} : $default) : $settings; // use default for empty settings
foreach($this->defaults as $name => $defaultValue) {
if(!property_exists($settings, $name) || empty($settings->{$name})) {
$settings->{$name} = $defaultValue;
}
}
} else {
$settings = (object)$this->defaults;
}
return $settings;
}
/**
* @throws \OCP\PreConditionNotMetException
*/
public function get($name) {
$settings = $this->getAll();
if(property_exists($settings, $name)) {
return $settings->{$name};
} else {
throw new \OCP\PreConditionNotMetException('Setting '.$name.' not found.');
}
} }
} }

View File

@ -80,8 +80,14 @@ style('notes', [
<button class="settings-button" data-apps-slide-toggle="#app-settings-content"><?php p($l->t('Settings'));?></button> <button class="settings-button" data-apps-slide-toggle="#app-settings-content"><?php p($l->t('Settings'));?></button>
</div> </div>
<div id="app-settings-content"> <div id="app-settings-content">
<p class="settings-hint"><label for="notesPath"><?php p($l->t('Folder to store your notes:')) ?></label></p> <div class="settings-notesPath">
<input type="text" name="notesPath" ng-model="settings.notesPath" placeholder="<?php p($l->t('path to notes')); ?>" id="notesPath" style="width:100%"/> <p class="settings-hint"><label for="notesPath"><?php p($l->t('Folder to store your notes')) ?></label></p>
<input type="text" name="notesPath" ng-model="settings.notesPath" placeholder="<?php p($l->t('path to notes')); ?>" id="notesPath" style="width:80%"/><input type="submit" class="icon-confirm" value="">
</div>
<div class="settings-fileSuffix">
<p class="settings-hint"><label for="fileSuffix"><?php p($l->t('File extension for new notes')) ?></label></p>
<select id="fileSuffix" ng-model="settings.fileSuffix" ng-options="o as o for o in extensions"></select><input type="submit" class="icon-confirm" value="">
</div>
</div> </div>
</div> </div>