diff --git a/app/Entities/Repos/PageRepo.php b/app/Entities/Repos/PageRepo.php index 6b004984f..ed142eb61 100644 --- a/app/Entities/Repos/PageRepo.php +++ b/app/Entities/Repos/PageRepo.php @@ -9,6 +9,7 @@ use Carbon\Carbon; use DOMDocument; use DOMElement; use DOMXPath; +use Illuminate\Support\Collection; class PageRepo extends EntityRepo { @@ -69,6 +70,10 @@ class PageRepo extends EntityRepo $this->tagRepo->saveTagsToEntity($page, $input['tags']); } + if (isset($input['template']) && userCan('templates-manage')) { + $page->template = ($input['template'] === 'true'); + } + // Update with new details $userId = user()->id; $page->fill($input); @@ -85,8 +90,9 @@ class PageRepo extends EntityRepo $this->userUpdatePageDraftsQuery($page, $userId)->delete(); // Save a revision after updating - if ($oldHtml !== $input['html'] || $oldName !== $input['name'] || $input['summary'] !== null) { - $this->savePageRevision($page, $input['summary']); + $summary = $input['summary'] ?? null; + if ($oldHtml !== $input['html'] || $oldName !== $input['name'] || $summary !== null) { + $this->savePageRevision($page, $summary); } $this->searchService->indexEntity($page); @@ -300,6 +306,10 @@ class PageRepo extends EntityRepo $this->tagRepo->saveTagsToEntity($draftPage, $input['tags']); } + if (isset($input['template']) && userCan('templates-manage')) { + $draftPage->template = ($input['template'] === 'true'); + } + $draftPage->slug = $this->findSuitableSlug('page', $draftPage->name, false, $draftPage->book->id); $draftPage->html = $this->formatHtml($input['html']); $draftPage->text = $this->pageToPlainText($draftPage); @@ -523,4 +533,29 @@ class PageRepo extends EntityRepo return $this->publishPageDraft($copyPage, $pageData); } + + /** + * Get pages that have been marked as templates. + * @param int $count + * @param int $page + * @param string $search + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator + */ + public function getPageTemplates(int $count = 10, int $page = 1, string $search = '') + { + $query = $this->entityQuery('page') + ->where('template', '=', true) + ->orderBy('name', 'asc') + ->skip( ($page - 1) * $count) + ->take($count); + + if ($search) { + $query->where('name', 'like', '%' . $search . '%'); + } + + $paginator = $query->paginate($count, ['*'], 'page', $page); + $paginator->withPath('/templates'); + + return $paginator; + } } diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index 89fb83fd9..8819510a6 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -110,11 +110,14 @@ class PageController extends Controller $this->setPageTitle(trans('entities.pages_edit_draft')); $draftsEnabled = $this->signedIn; + $templates = $this->pageRepo->getPageTemplates(10); + return view('pages.edit', [ 'page' => $draft, 'book' => $draft->book, 'isDraft' => true, - 'draftsEnabled' => $draftsEnabled + 'draftsEnabled' => $draftsEnabled, + 'templates' => $templates, ]); } @@ -239,11 +242,14 @@ class PageController extends Controller } $draftsEnabled = $this->signedIn; + $templates = $this->pageRepo->getPageTemplates(10); + return view('pages.edit', [ 'page' => $page, 'book' => $page->book, 'current' => $page, - 'draftsEnabled' => $draftsEnabled + 'draftsEnabled' => $draftsEnabled, + 'templates' => $templates, ]); } diff --git a/app/Http/Controllers/PageTemplateController.php b/app/Http/Controllers/PageTemplateController.php new file mode 100644 index 000000000..05943351a --- /dev/null +++ b/app/Http/Controllers/PageTemplateController.php @@ -0,0 +1,63 @@ +pageRepo = $pageRepo; + parent::__construct(); + } + + /** + * Fetch a list of templates from the system. + * @param Request $request + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + */ + public function list(Request $request) + { + $page = $request->get('page', 1); + $search = $request->get('search', ''); + $templates = $this->pageRepo->getPageTemplates(10, $page, $search); + + if ($search) { + $templates->appends(['search' => $search]); + } + + return view('pages.template-manager-list', [ + 'templates' => $templates + ]); + } + + /** + * Get the content of a template. + * @param $templateId + * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response + * @throws NotFoundException + */ + public function get($templateId) + { + $page = $this->pageRepo->getById('page', $templateId); + + if (!$page->template) { + throw new NotFoundException(); + } + + return response()->json([ + 'html' => $page->html, + 'markdown' => $page->markdown, + ]); + } + +} diff --git a/database/migrations/2019_07_07_112515_add_template_support.php b/database/migrations/2019_07_07_112515_add_template_support.php new file mode 100644 index 000000000..a54508198 --- /dev/null +++ b/database/migrations/2019_07_07_112515_add_template_support.php @@ -0,0 +1,54 @@ +boolean('template')->default(false); + $table->index('template'); + }); + + // Create new templates-manage permission and assign to admin role + $adminRoleId = DB::table('roles')->where('system_name', '=', 'admin')->first()->id; + $permissionId = DB::table('role_permissions')->insertGetId([ + 'name' => 'templates-manage', + 'display_name' => 'Manage Page Templates', + 'created_at' => Carbon::now()->toDateTimeString(), + 'updated_at' => Carbon::now()->toDateTimeString() + ]); + DB::table('permission_role')->insert([ + 'role_id' => $adminRoleId, + 'permission_id' => $permissionId + ]); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('pages', function (Blueprint $table) { + $table->dropColumn('template'); + }); + + // Remove templates-manage permission + $templatesManagePermission = DB::table('role_permissions') + ->where('name', '=', 'templates_manage')->first(); + + DB::table('permission_role')->where('permission_id', '=', $templatesManagePermission->id)->delete(); + DB::table('role_permissions')->where('name', '=', 'templates_manage')->delete(); + } +} diff --git a/readme.md b/readme.md index 276a3de20..62e2aa65d 100644 --- a/readme.md +++ b/readme.md @@ -3,6 +3,7 @@ [![GitHub release](https://img.shields.io/github/release/BookStackApp/BookStack.svg)](https://github.com/BookStackApp/BookStack/releases/latest) [![license](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/BookStackApp/BookStack/blob/master/LICENSE) [![Build Status](https://travis-ci.org/BookStackApp/BookStack.svg)](https://travis-ci.org/BookStackApp/BookStack) +[![Discord](https://img.shields.io/static/v1?label=Chat&message=Discord&color=738adb&logo=discord)](https://discord.gg/ztkBqR2) A platform for storing and organising information and documentation. General information and documentation for BookStack can be found at https://www.bookstackapp.com/. diff --git a/resources/assets/icons/chevron-down.svg b/resources/assets/icons/chevron-down.svg new file mode 100644 index 000000000..f08dfafcb --- /dev/null +++ b/resources/assets/icons/chevron-down.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources/assets/icons/template.svg b/resources/assets/icons/template.svg new file mode 100644 index 000000000..7c142124f --- /dev/null +++ b/resources/assets/icons/template.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/resources/assets/js/components/index.js b/resources/assets/js/components/index.js index 1c2abd520..8c12da9b4 100644 --- a/resources/assets/js/components/index.js +++ b/resources/assets/js/components/index.js @@ -27,6 +27,7 @@ import customCheckbox from "./custom-checkbox"; import bookSort from "./book-sort"; import settingAppColorPicker from "./setting-app-color-picker"; import entityPermissionsEditor from "./entity-permissions-editor"; +import templateManager from "./template-manager"; const componentMapping = { 'dropdown': dropdown, @@ -57,7 +58,8 @@ const componentMapping = { 'custom-checkbox': customCheckbox, 'book-sort': bookSort, 'setting-app-color-picker': settingAppColorPicker, - 'entity-permissions-editor': entityPermissionsEditor + 'entity-permissions-editor': entityPermissionsEditor, + 'template-manager': templateManager, }; window.components = {}; diff --git a/resources/assets/js/components/markdown-editor.js b/resources/assets/js/components/markdown-editor.js index b0e4d693a..7cb56eef8 100644 --- a/resources/assets/js/components/markdown-editor.js +++ b/resources/assets/js/components/markdown-editor.js @@ -91,6 +91,7 @@ class MarkdownEditor { }); this.codeMirrorSetup(); + this.listenForBookStackEditorEvents(); } // Update the input content and render the display. @@ -461,6 +462,37 @@ class MarkdownEditor { }) } + listenForBookStackEditorEvents() { + + function getContentToInsert({html, markdown}) { + return markdown || html; + } + + // Replace editor content + window.$events.listen('editor::replace', (eventContent) => { + const markdown = getContentToInsert(eventContent); + this.cm.setValue(markdown); + }); + + // Append editor content + window.$events.listen('editor::append', (eventContent) => { + const cursorPos = this.cm.getCursor('from'); + const markdown = getContentToInsert(eventContent); + const content = this.cm.getValue() + '\n' + markdown; + this.cm.setValue(content); + this.cm.setCursor(cursorPos.line, cursorPos.ch); + }); + + // Prepend editor content + window.$events.listen('editor::prepend', (eventContent) => { + const cursorPos = this.cm.getCursor('from'); + const markdown = getContentToInsert(eventContent); + const content = markdown + '\n' + this.cm.getValue(); + this.cm.setValue(content); + const prependLineCount = markdown.split('\n').length; + this.cm.setCursor(cursorPos.line + prependLineCount, cursorPos.ch); + }); + } } export default MarkdownEditor ; diff --git a/resources/assets/js/components/template-manager.js b/resources/assets/js/components/template-manager.js new file mode 100644 index 000000000..b966762d2 --- /dev/null +++ b/resources/assets/js/components/template-manager.js @@ -0,0 +1,85 @@ +import * as DOM from "../services/dom"; + +class TemplateManager { + + constructor(elem) { + this.elem = elem; + this.list = elem.querySelector('[template-manager-list]'); + this.searching = false; + + // Template insert action buttons + DOM.onChildEvent(this.elem, '[template-action]', 'click', this.handleTemplateActionClick.bind(this)); + + // Template list pagination click + DOM.onChildEvent(this.elem, '.pagination a', 'click', this.handlePaginationClick.bind(this)); + + // Template list item content click + DOM.onChildEvent(this.elem, '.template-item-content', 'click', this.handleTemplateItemClick.bind(this)); + + this.setupSearchBox(); + } + + handleTemplateItemClick(event, templateItem) { + const templateId = templateItem.closest('[template-id]').getAttribute('template-id'); + this.insertTemplate(templateId, 'replace'); + } + + handleTemplateActionClick(event, actionButton) { + event.stopPropagation(); + + const action = actionButton.getAttribute('template-action'); + const templateId = actionButton.closest('[template-id]').getAttribute('template-id'); + this.insertTemplate(templateId, action); + } + + async insertTemplate(templateId, action = 'replace') { + const resp = await window.$http.get(`/templates/${templateId}`); + const eventName = 'editor::' + action; + window.$events.emit(eventName, resp.data); + } + + async handlePaginationClick(event, paginationLink) { + event.preventDefault(); + const paginationUrl = paginationLink.getAttribute('href'); + const resp = await window.$http.get(paginationUrl); + this.list.innerHTML = resp.data; + } + + setupSearchBox() { + const searchBox = this.elem.querySelector('.search-box'); + const input = searchBox.querySelector('input'); + const submitButton = searchBox.querySelector('button'); + const cancelButton = searchBox.querySelector('button.search-box-cancel'); + + async function performSearch() { + const searchTerm = input.value; + const resp = await window.$http.get(`/templates`, { + search: searchTerm + }); + cancelButton.style.display = searchTerm ? 'block' : 'none'; + this.list.innerHTML = resp.data; + } + performSearch = performSearch.bind(this); + + // Searchbox enter press + searchBox.addEventListener('keypress', event => { + if (event.key === 'Enter') { + event.preventDefault(); + performSearch(); + } + }); + + // Submit button press + submitButton.addEventListener('click', event => { + performSearch(); + }); + + // Cancel button press + cancelButton.addEventListener('click', event => { + input.value = ''; + performSearch(); + }); + } +} + +export default TemplateManager; \ No newline at end of file diff --git a/resources/assets/js/components/wysiwyg-editor.js b/resources/assets/js/components/wysiwyg-editor.js index eb9f025a7..be0aaf18a 100644 --- a/resources/assets/js/components/wysiwyg-editor.js +++ b/resources/assets/js/components/wysiwyg-editor.js @@ -378,6 +378,27 @@ function customHrPlugin() { } +function listenForBookStackEditorEvents(editor) { + + // Replace editor content + window.$events.listen('editor::replace', ({html}) => { + editor.setContent(html); + }); + + // Append editor content + window.$events.listen('editor::append', ({html}) => { + const content = editor.getContent() + html; + editor.setContent(content); + }); + + // Prepend editor content + window.$events.listen('editor::prepend', ({html}) => { + const content = html + editor.getContent(); + editor.setContent(content); + }); + +} + class WysiwygEditor { constructor(elem) { @@ -553,6 +574,10 @@ class WysiwygEditor { editor.focus(); } + listenForBookStackEditorEvents(editor); + + // TODO - Update to standardise across both editors + // Use events within listenForBookStackEditorEvents instead (Different event signature) window.$events.listen('editor-html-update', html => { editor.setContent(html); editor.selection.select(editor.getBody(), true); diff --git a/resources/assets/sass/_blocks.scss b/resources/assets/sass/_blocks.scss index 032b1cbeb..5f11c2355 100644 --- a/resources/assets/sass/_blocks.scss +++ b/resources/assets/sass/_blocks.scss @@ -83,6 +83,10 @@ line-height: 1; } +.card.border-card { + border: 1px solid #DDD; +} + .card.drag-card { border: 1px solid #DDD; border-radius: 4px; diff --git a/resources/assets/sass/_components.scss b/resources/assets/sass/_components.scss index 039ac4dc8..0b683c6e3 100644 --- a/resources/assets/sass/_components.scss +++ b/resources/assets/sass/_components.scss @@ -655,4 +655,32 @@ body.flexbox-support #entity-selector-wrap .popup-body .form-group { } .permissions-table tr:hover [permissions-table-toggle-all-in-row] { display: inline; +} + +.template-item { + cursor: pointer; + position: relative; + &:hover, .template-item-actions button:hover { + background-color: #F2F2F2; + } + .template-item-actions { + position: absolute; + top: 0; + right: 0; + width: 50px; + height: 100%; + display: flex; + flex-direction: column; + border-left: 1px solid #DDD; + } + .template-item-actions button { + cursor: pointer; + flex: 1; + background: #FFF; + border: 0; + border-top: 1px solid #DDD; + } + .template-item-actions button:first-child { + border-top: 0; + } } \ No newline at end of file diff --git a/resources/assets/sass/_pages.scss b/resources/assets/sass/_pages.scss index fc784eebe..be0cc381c 100755 --- a/resources/assets/sass/_pages.scss +++ b/resources/assets/sass/_pages.scss @@ -262,7 +262,7 @@ body.mce-fullscreen .page-editor .edit-area { display: block; cursor: pointer; padding: $-s $-m; - font-size: 13.5px; + font-size: 16px; line-height: 1.6; border-bottom: 1px solid rgba(255, 255, 255, 0.3); } diff --git a/resources/lang/en/entities.php b/resources/lang/en/entities.php index f6df7e71b..3208a6dfc 100644 --- a/resources/lang/en/entities.php +++ b/resources/lang/en/entities.php @@ -233,6 +233,7 @@ return [ ], 'pages_draft_discarded' => 'Draft discarded, The editor has been updated with the current page content', 'pages_specific' => 'Specific Page', + 'pages_is_template' => 'Page Template', // Editor Sidebar 'page_tags' => 'Page Tags', @@ -269,6 +270,12 @@ return [ 'attachments_file_uploaded' => 'File successfully uploaded', 'attachments_file_updated' => 'File successfully updated', 'attachments_link_attached' => 'Link successfully attached to page', + 'templates' => 'Templates', + 'templates_set_as_template' => 'Page is a template', + 'templates_explain_set_as_template' => 'You can set this page as a template so its contents be utilized when creating other pages. Other users will be able to use this template if they have view permissions for this page.', + 'templates_replace_content' => 'Replace page content', + 'templates_append_content' => 'Append to page content', + 'templates_prepend_content' => 'Prepend to page content', // Profile View 'profile_user_for_x' => 'User for :time', diff --git a/resources/lang/en/settings.php b/resources/lang/en/settings.php index d275e330a..78f86b68e 100755 --- a/resources/lang/en/settings.php +++ b/resources/lang/en/settings.php @@ -85,6 +85,7 @@ return [ 'role_manage_roles' => 'Manage roles & role permissions', 'role_manage_entity_permissions' => 'Manage all book, chapter & page permissions', 'role_manage_own_entity_permissions' => 'Manage permissions on own book, chapter & pages', + 'role_manage_page_templates' => 'Manage page templates', 'role_manage_settings' => 'Manage app settings', 'role_asset' => 'Asset Permissions', 'role_asset_desc' => 'These permissions control default access to the assets within the system. Permissions on Books, Chapters and Pages will override these permissions.', diff --git a/resources/views/pages/attachment-manager.blade.php b/resources/views/pages/attachment-manager.blade.php new file mode 100644 index 000000000..7b16c6b7b --- /dev/null +++ b/resources/views/pages/attachment-manager.blade.php @@ -0,0 +1,99 @@ +
{{ trans('entities.attachments_explain') }} {{ trans('entities.attachments_explain_instant_save') }}
+ + + + +{{ trans('entities.attachments_explain') }} {{ trans('entities.attachments_explain_instant_save') }}
- - - - -+ {{ trans('entities.templates_explain_set_as_template') }} +
+ @include('components.toggle-switch', [ + 'name' => 'template', + 'value' => old('template', $page->template ? 'true' : 'false') === 'true', + 'label' => trans('entities.templates_set_as_template') + ]) +