]> BookStack Code Mirror - bookstack/commitdiff
Merge pull request #1 from BookStackApp/master
authorJames Geiger <redacted>
Wed, 14 Aug 2019 06:10:43 +0000 (06:10 +0000)
committerGitHub <redacted>
Wed, 14 Aug 2019 06:10:43 +0000 (06:10 +0000)
merge aug. 11 commits

26 files changed:
app/Entities/Repos/PageRepo.php
app/Http/Controllers/PageController.php
app/Http/Controllers/PageTemplateController.php [new file with mode: 0644]
database/migrations/2019_07_07_112515_add_template_support.php [new file with mode: 0644]
readme.md
resources/assets/icons/chevron-down.svg [new file with mode: 0644]
resources/assets/icons/template.svg [new file with mode: 0644]
resources/assets/js/components/index.js
resources/assets/js/components/markdown-editor.js
resources/assets/js/components/template-manager.js [new file with mode: 0644]
resources/assets/js/components/wysiwyg-editor.js
resources/assets/sass/_blocks.scss
resources/assets/sass/_components.scss
resources/assets/sass/_pages.scss
resources/lang/en/entities.php
resources/lang/en/settings.php
resources/views/pages/attachment-manager.blade.php [new file with mode: 0644]
resources/views/pages/edit.blade.php
resources/views/pages/editor-toolbox.blade.php [new file with mode: 0644]
resources/views/pages/form-toolbox.blade.php [deleted file]
resources/views/pages/show.blade.php
resources/views/pages/template-manager-list.blade.php [new file with mode: 0644]
resources/views/pages/template-manager.blade.php [new file with mode: 0644]
resources/views/settings/roles/form.blade.php
routes/web.php
tests/Entity/PageTemplateTest.php [new file with mode: 0644]

index 6b004984f389e5f7d553ccbf366bf23ef148e292..ed142eb611b53f3d715722ded3bf12bdb2e4455f 100644 (file)
@@ -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;
+    }
 }
index 89fb83fd97f116b2005827b30cce0ff459d81e5b..8819510a6d4388bd600e0c89bee5e9038a09fbda 100644 (file)
@@ -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 (file)
index 0000000..0594335
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+
+namespace BookStack\Http\Controllers;
+
+use BookStack\Entities\Repos\PageRepo;
+use BookStack\Exceptions\NotFoundException;
+use Illuminate\Http\Request;
+
+class PageTemplateController extends Controller
+{
+    protected $pageRepo;
+
+    /**
+     * PageTemplateController constructor.
+     * @param $pageRepo
+     */
+    public function __construct(PageRepo $pageRepo)
+    {
+        $this->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 (file)
index 0000000..a545081
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+use Carbon\Carbon;
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddTemplateSupport extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('pages', function (Blueprint $table) {
+            $table->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();
+    }
+}
index 276a3de2017e35fdf3856c55ba4826a8054ccfe9..62e2aa65d8abf959721ab412cec07a524f9e6888 100644 (file)
--- 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 (file)
index 0000000..f08dfaf
--- /dev/null
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M7.41 8L12 12.58 16.59 8 18 9.41l-6 6-6-6z"/><path d="M0 0h24v24H0z" fill="none"/></svg>
\ No newline at end of file
diff --git a/resources/assets/icons/template.svg b/resources/assets/icons/template.svg
new file mode 100644 (file)
index 0000000..7c14212
--- /dev/null
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 4h7V2H4c-1.1 0-2 .9-2 2v7h2zm16-2h-7v2h7v7h2V4c0-1.1-.9-2-2-2zm0 18h-7v2h7c1.1 0 2-.9 2-2v-7h-2zM4 13H2v7c0 1.1.9 2 2 2h7v-2H4zM16.475 15.356h-8.95v-2.237h8.95zm0-4.475h-8.95V8.644h8.95z"/></svg>
\ No newline at end of file
index 1c2abd5202e1b47b964cbb5d0239fddddf12019e..8c12da9b44506fb3beeb8785afe03c11495d362d 100644 (file)
@@ -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 = {};
index b0e4d693a4e499810ad5bb40e549815095a9b8ec..7cb56eef831ee5fb75636a1f91345a505dd528a8 100644 (file)
@@ -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 (file)
index 0000000..b966762
--- /dev/null
@@ -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
index eb9f025a749d91edf62fbad3d8be131892523120..be0aaf18a1ebc742cd1adabe4b413e3466cbcaab 100644 (file)
@@ -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);
index 032b1cbeb1c467983eaf8da8b1e02c5b0c38d0f2..5f11c235532865e3c3325264e69e4ccf104ead61 100644 (file)
   line-height: 1;
 }
 
+.card.border-card {
+  border: 1px solid #DDD;
+}
+
 .card.drag-card {
   border: 1px solid #DDD;
   border-radius: 4px;
index 039ac4dc8d8c3580c7f0374025ac6aa43a0692e3..0b683c6e315f3f9f6bdd70e0b8313621a353fc24 100644 (file)
@@ -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
index fc784eebe84bcf5a6d745cc5cb3be0ef8048cc2d..be0cc381c828cca940afb1f505937763f08a5f7a 100755 (executable)
@@ -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);
   }
index f6df7e71b308db293da1844e3aa8682794f08ffc..3208a6dfcc212f339b47e9a573933cd3c7233137 100644 (file)
@@ -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',
index d275e330a480b00bfde576d31bf28ed9fd7dcb57..78f86b68ea3c3e9c198b90d51ddcf5915553dfed 100755 (executable)
@@ -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 (file)
index 0000000..7b16c6b
--- /dev/null
@@ -0,0 +1,99 @@
+<div toolbox-tab-content="files" id="attachment-manager" page-id="{{ $page->id ?? 0 }}">
+
+    @exposeTranslations([
+    'entities.attachments_file_uploaded',
+    'entities.attachments_file_updated',
+    'entities.attachments_link_attached',
+    'entities.attachments_updated_success',
+    'errors.server_upload_limit',
+    'components.image_upload_remove',
+    'components.file_upload_timeout',
+    ])
+
+    <h4>{{ trans('entities.attachments') }}</h4>
+    <div class="px-l files">
+
+        <div id="file-list" v-show="!fileToEdit">
+            <p class="text-muted small">{{ trans('entities.attachments_explain') }} <span class="text-warn">{{ trans('entities.attachments_explain_instant_save') }}</span></p>
+
+            <div class="tab-container">
+                <div class="nav-tabs">
+                    <div @click="tab = 'list'" :class="{selected: tab === 'list'}" class="tab-item">{{ trans('entities.attachments_items') }}</div>
+                    <div @click="tab = 'file'" :class="{selected: tab === 'file'}" class="tab-item">{{ trans('entities.attachments_upload') }}</div>
+                    <div @click="tab = 'link'" :class="{selected: tab === 'link'}" class="tab-item">{{ trans('entities.attachments_link') }}</div>
+                </div>
+                <div v-show="tab === 'list'">
+                    <draggable style="width: 100%;" :options="{handle: '.handle'}" @change="fileSortUpdate" :list="files" element="div">
+                        <div v-for="(file, index) in files" :key="file.id" class="card drag-card">
+                            <div class="handle">@icon('grip')</div>
+                            <div class="py-s">
+                                <a :href="getFileUrl(file)" target="_blank" v-text="file.name"></a>
+                                <div v-if="file.deleting">
+                                    <span class="text-neg small">{{ trans('entities.attachments_delete_confirm') }}</span>
+                                    <br>
+                                    <span class="text-primary small" @click="file.deleting = false;">{{ trans('common.cancel') }}</span>
+                                </div>
+                            </div>
+                            <div @click="startEdit(file)" class="drag-card-action text-center text-primary">@icon('edit')</div>
+                            <div @click="deleteFile(file)" class="drag-card-action text-center text-neg">@icon('close')</div>
+                        </div>
+                    </draggable>
+                    <p class="small text-muted" v-if="files.length === 0">
+                        {{ trans('entities.attachments_no_files') }}
+                    </p>
+                </div>
+                <div v-show="tab === 'file'">
+                    <dropzone placeholder="{{ trans('entities.attachments_dropzone') }}" :upload-url="getUploadUrl()" :uploaded-to="pageId" @success="uploadSuccess"></dropzone>
+                </div>
+                <div v-show="tab === 'link'" @keypress.enter.prevent="attachNewLink(file)">
+                    <p class="text-muted small">{{ trans('entities.attachments_explain_link') }}</p>
+                    <div class="form-group">
+                        <label for="attachment-via-link">{{ trans('entities.attachments_link_name') }}</label>
+                        <input type="text" placeholder="{{ trans('entities.attachments_link_name') }}" v-model="file.name">
+                        <p class="small text-neg" v-for="error in errors.link.name" v-text="error"></p>
+                    </div>
+                    <div class="form-group">
+                        <label for="attachment-via-link">{{ trans('entities.attachments_link_url') }}</label>
+                        <input type="text"  placeholder="{{ trans('entities.attachments_link_url_hint') }}" v-model="file.link">
+                        <p class="small text-neg" v-for="error in errors.link.link" v-text="error"></p>
+                    </div>
+                    <button @click.prevent="attachNewLink(file)" class="button primary">{{ trans('entities.attach') }}</button>
+
+                </div>
+            </div>
+
+        </div>
+
+        <div id="file-edit" v-if="fileToEdit" @keypress.enter.prevent="updateFile(fileToEdit)">
+            <h5>{{ trans('entities.attachments_edit_file') }}</h5>
+
+            <div class="form-group">
+                <label for="attachment-name-edit">{{ trans('entities.attachments_edit_file_name') }}</label>
+                <input type="text" id="attachment-name-edit" placeholder="{{ trans('entities.attachments_edit_file_name') }}" v-model="fileToEdit.name">
+                <p class="small text-neg" v-for="error in errors.edit.name" v-text="error"></p>
+            </div>
+
+            <div class="tab-container">
+                <div class="nav-tabs">
+                    <div @click="editTab = 'file'" :class="{selected: editTab === 'file'}" class="tab-item">{{ trans('entities.attachments_upload') }}</div>
+                    <div @click="editTab = 'link'" :class="{selected: editTab === 'link'}" class="tab-item">{{ trans('entities.attachments_set_link') }}</div>
+                </div>
+                <div v-if="editTab === 'file'">
+                    <dropzone :upload-url="getUploadUrl(fileToEdit)" :uploaded-to="pageId" placeholder="{{ trans('entities.attachments_edit_drop_upload') }}" @success="uploadSuccessUpdate"></dropzone>
+                    <br>
+                </div>
+                <div v-if="editTab === 'link'">
+                    <div class="form-group">
+                        <label for="attachment-link-edit">{{ trans('entities.attachments_link_url') }}</label>
+                        <input type="text" id="attachment-link-edit" placeholder="{{ trans('entities.attachment_link') }}" v-model="fileToEdit.link">
+                        <p class="small text-neg" v-for="error in errors.edit.link" v-text="error"></p>
+                    </div>
+                </div>
+            </div>
+
+            <button type="button" class="button outline" @click="cancelEdit">{{ trans('common.back') }}</button>
+            <button @click.enter.prevent="updateFile(fileToEdit)" class="button primary">{{ trans('common.save') }}</button>
+        </div>
+
+    </div>
+</div>
\ No newline at end of file
index 4930e30a3d5710628cd017d2c7b2350b689b4b37..cfb66fdd0e34ad87827be468764670b230bc2a3b 100644 (file)
@@ -16,7 +16,7 @@
                 <input type="hidden" name="_method" value="PUT">
             @endif
             @include('pages.form', ['model' => $page])
-            @include('pages.form-toolbox')
+            @include('pages.editor-toolbox')
         </form>
     </div>
     
diff --git a/resources/views/pages/editor-toolbox.blade.php b/resources/views/pages/editor-toolbox.blade.php
new file mode 100644 (file)
index 0000000..3ce4cfb
--- /dev/null
@@ -0,0 +1,32 @@
+<div editor-toolbox class="floating-toolbox">
+
+    <div class="tabs primary-background-light">
+        <span toolbox-toggle>@icon('caret-left-circle')</span>
+        <span toolbox-tab-button="tags" title="{{ trans('entities.page_tags') }}" class="active">@icon('tag')</span>
+        @if(userCan('attachment-create-all'))
+            <span toolbox-tab-button="files" title="{{ trans('entities.attachments') }}">@icon('attach')</span>
+        @endif
+        <span toolbox-tab-button="templates" title="{{ trans('entities.templates') }}">@icon('template')</span>
+    </div>
+
+    <div toolbox-tab-content="tags">
+        <h4>{{ trans('entities.page_tags') }}</h4>
+        <div class="px-l">
+            @include('components.tag-manager', ['entity' => $page, 'entityType' => 'page'])
+        </div>
+    </div>
+
+    @if(userCan('attachment-create-all'))
+        @include('pages.attachment-manager', ['page' => $page])
+    @endif
+
+    <div toolbox-tab-content="templates">
+        <h4>{{ trans('entities.templates') }}</h4>
+
+        <div class="px-l">
+            @include('pages.template-manager', ['page' => $page, 'templates' => $templates])
+        </div>
+
+    </div>
+
+</div>
diff --git a/resources/views/pages/form-toolbox.blade.php b/resources/views/pages/form-toolbox.blade.php
deleted file mode 100644 (file)
index d69be20..0000000
+++ /dev/null
@@ -1,121 +0,0 @@
-
-<div editor-toolbox class="floating-toolbox">
-
-    <div class="tabs primary-background-light">
-        <span toolbox-toggle>@icon('caret-left-circle')</span>
-        <span toolbox-tab-button="tags" title="{{ trans('entities.page_tags') }}" class="active">@icon('tag')</span>
-        @if(userCan('attachment-create-all'))
-            <span toolbox-tab-button="files" title="{{ trans('entities.attachments') }}">@icon('attach')</span>
-        @endif
-    </div>
-
-    <div toolbox-tab-content="tags">
-        <h4>{{ trans('entities.page_tags') }}</h4>
-        <div class="px-l">
-            @include('components.tag-manager', ['entity' => $page, 'entityType' => 'page'])
-        </div>
-    </div>
-
-    @if(userCan('attachment-create-all'))
-        <div toolbox-tab-content="files" id="attachment-manager" page-id="{{ $page->id ?? 0 }}">
-
-            @exposeTranslations([
-                'entities.attachments_file_uploaded',
-                'entities.attachments_file_updated',
-                'entities.attachments_link_attached',
-                'entities.attachments_updated_success',
-                'errors.server_upload_limit',
-                'components.image_upload_remove',
-                'components.file_upload_timeout',
-            ])
-
-            <h4>{{ trans('entities.attachments') }}</h4>
-            <div class="px-l files">
-
-                <div id="file-list" v-show="!fileToEdit">
-                    <p class="text-muted small">{{ trans('entities.attachments_explain') }} <span class="text-warn">{{ trans('entities.attachments_explain_instant_save') }}</span></p>
-
-                    <div class="tab-container">
-                        <div class="nav-tabs">
-                            <div @click="tab = 'list'" :class="{selected: tab === 'list'}" class="tab-item">{{ trans('entities.attachments_items') }}</div>
-                            <div @click="tab = 'file'" :class="{selected: tab === 'file'}" class="tab-item">{{ trans('entities.attachments_upload') }}</div>
-                            <div @click="tab = 'link'" :class="{selected: tab === 'link'}" class="tab-item">{{ trans('entities.attachments_link') }}</div>
-                        </div>
-                        <div v-show="tab === 'list'">
-                            <draggable style="width: 100%;" :options="{handle: '.handle'}" @change="fileSortUpdate" :list="files" element="div">
-                                <div v-for="(file, index) in files" :key="file.id" class="card drag-card">
-                                    <div class="handle">@icon('grip')</div>
-                                    <div class="py-s">
-                                        <a :href="getFileUrl(file)" target="_blank" v-text="file.name"></a>
-                                        <div v-if="file.deleting">
-                                            <span class="text-neg small">{{ trans('entities.attachments_delete_confirm') }}</span>
-                                            <br>
-                                            <span class="text-primary small" @click="file.deleting = false;">{{ trans('common.cancel') }}</span>
-                                        </div>
-                                    </div>
-                                    <div @click="startEdit(file)" class="drag-card-action text-center text-primary">@icon('edit')</div>
-                                    <div @click="deleteFile(file)" class="drag-card-action text-center text-neg">@icon('close')</div>
-                                </div>
-                            </draggable>
-                            <p class="small text-muted" v-if="files.length === 0">
-                                {{ trans('entities.attachments_no_files') }}
-                            </p>
-                        </div>
-                        <div v-show="tab === 'file'">
-                            <dropzone placeholder="{{ trans('entities.attachments_dropzone') }}" :upload-url="getUploadUrl()" :uploaded-to="pageId" @success="uploadSuccess"></dropzone>
-                        </div>
-                        <div v-show="tab === 'link'" @keypress.enter.prevent="attachNewLink(file)">
-                            <p class="text-muted small">{{ trans('entities.attachments_explain_link') }}</p>
-                            <div class="form-group">
-                                <label for="attachment-via-link">{{ trans('entities.attachments_link_name') }}</label>
-                                <input type="text" placeholder="{{ trans('entities.attachments_link_name') }}" v-model="file.name">
-                                <p class="small text-neg" v-for="error in errors.link.name" v-text="error"></p>
-                            </div>
-                            <div class="form-group">
-                                <label for="attachment-via-link">{{ trans('entities.attachments_link_url') }}</label>
-                                <input type="text"  placeholder="{{ trans('entities.attachments_link_url_hint') }}" v-model="file.link">
-                                <p class="small text-neg" v-for="error in errors.link.link" v-text="error"></p>
-                            </div>
-                            <button @click.prevent="attachNewLink(file)" class="button primary">{{ trans('entities.attach') }}</button>
-
-                        </div>
-                    </div>
-
-                </div>
-
-                <div id="file-edit" v-if="fileToEdit" @keypress.enter.prevent="updateFile(fileToEdit)">
-                    <h5>{{ trans('entities.attachments_edit_file') }}</h5>
-
-                    <div class="form-group">
-                        <label for="attachment-name-edit">{{ trans('entities.attachments_edit_file_name') }}</label>
-                        <input type="text" id="attachment-name-edit" placeholder="{{ trans('entities.attachments_edit_file_name') }}" v-model="fileToEdit.name">
-                        <p class="small text-neg" v-for="error in errors.edit.name" v-text="error"></p>
-                    </div>
-
-                    <div class="tab-container">
-                        <div class="nav-tabs">
-                            <div @click="editTab = 'file'" :class="{selected: editTab === 'file'}" class="tab-item">{{ trans('entities.attachments_upload') }}</div>
-                            <div @click="editTab = 'link'" :class="{selected: editTab === 'link'}" class="tab-item">{{ trans('entities.attachments_set_link') }}</div>
-                        </div>
-                        <div v-if="editTab === 'file'">
-                            <dropzone :upload-url="getUploadUrl(fileToEdit)" :uploaded-to="pageId" placeholder="{{ trans('entities.attachments_edit_drop_upload') }}" @success="uploadSuccessUpdate"></dropzone>
-                            <br>
-                        </div>
-                        <div v-if="editTab === 'link'">
-                            <div class="form-group">
-                                <label for="attachment-link-edit">{{ trans('entities.attachments_link_url') }}</label>
-                                <input type="text" id="attachment-link-edit" placeholder="{{ trans('entities.attachment_link') }}" v-model="fileToEdit.link">
-                                <p class="small text-neg" v-for="error in errors.edit.link" v-text="error"></p>
-                            </div>
-                        </div>
-                    </div>
-
-                    <button type="button" class="button outline" @click="cancelEdit">{{ trans('common.back') }}</button>
-                    <button @click.enter.prevent="updateFile(fileToEdit)" class="button primary">{{ trans('common.save') }}</button>
-                </div>
-
-            </div>
-        </div>
-    @endif
-
-</div>
index fb0df2ddd027e6e6c0c880f7c58271f8f12282c7..86b0d3f88d39020f7f5f03c34e8b8473f66c309d 100644 (file)
                     @endif
                 </div>
             @endif
+
+            @if($page->template)
+                <div>
+                    @icon('template'){{ trans('entities.pages_is_template') }}
+                </div>
+            @endif
         </div>
     </div>
 
diff --git a/resources/views/pages/template-manager-list.blade.php b/resources/views/pages/template-manager-list.blade.php
new file mode 100644 (file)
index 0000000..68899c8
--- /dev/null
@@ -0,0 +1,20 @@
+{{ $templates->links() }}
+
+@foreach($templates as $template)
+    <div class="card template-item border-card p-m mb-m" draggable="true" template-id="{{ $template->id }}">
+        <div class="template-item-content" title="{{ trans('entities.templates_replace_content') }}">
+            <div>{{ $template->name }}</div>
+            <div class="text-muted">{{ trans('entities.meta_updated', ['timeLength' => $template->updated_at->diffForHumans()]) }}</div>
+        </div>
+        <div class="template-item-actions">
+            <button type="button"
+                    title="{{ trans('entities.templates_prepend_content') }}"
+                    template-action="prepend">@icon('chevron-up')</button>
+            <button type="button"
+                    title="{{ trans('entities.templates_append_content') }}"
+                    template-action="append">@icon('chevron-down')</button>
+        </div>
+    </div>
+@endforeach
+
+{{ $templates->links() }}
\ No newline at end of file
diff --git a/resources/views/pages/template-manager.blade.php b/resources/views/pages/template-manager.blade.php
new file mode 100644 (file)
index 0000000..fbdb70a
--- /dev/null
@@ -0,0 +1,25 @@
+<div template-manager>
+    @if(userCan('templates-manage'))
+        <p class="text-muted small mb-none">
+            {{ trans('entities.templates_explain_set_as_template') }}
+        </p>
+        @include('components.toggle-switch', [
+               'name' => 'template',
+               'value' => old('template', $page->template ? 'true' : 'false') === 'true',
+               'label' => trans('entities.templates_set_as_template')
+        ])
+        <hr>
+    @endif
+
+    @if(count($templates) > 0)
+        <div class="search-box flexible mb-m">
+            <input type="text" name="template-search" placeholder="{{ trans('common.search') }}">
+            <button type="button">@icon('search')</button>
+            <button class="search-box-cancel text-neg hidden" type="button">@icon('close')</button>
+        </div>
+    @endif
+
+    <div template-manager-list>
+        @include('pages.template-manager-list', ['templates' => $templates])
+    </div>
+</div>
\ No newline at end of file
index 68b841e034d8be9e09f896f24386eb31960713ea..a9933a7a6c5d749a7dc9612a5b8790b574e53f95 100644 (file)
@@ -38,6 +38,7 @@
                 <div>@include('settings.roles.checkbox', ['permission' => 'user-roles-manage', 'label' => trans('settings.role_manage_roles')])</div>
                 <div>@include('settings.roles.checkbox', ['permission' => 'restrictions-manage-all', 'label' => trans('settings.role_manage_entity_permissions')])</div>
                 <div>@include('settings.roles.checkbox', ['permission' => 'restrictions-manage-own', 'label' => trans('settings.role_manage_own_entity_permissions')])</div>
+                <div>@include('settings.roles.checkbox', ['permission' => 'templates-manage', 'label' => trans('settings.role_manage_page_templates')])</div>
                 <div>@include('settings.roles.checkbox', ['permission' => 'settings-manage', 'label' => trans('settings.role_manage_settings')])</div>
             </div>
         </div>
index 94dd576fe2e928a0229123013e14b824d9b14bdb..11ca5d1af9066285d529e59ce42c9529752d9654 100644 (file)
@@ -158,6 +158,9 @@ Route::group(['middleware' => 'auth'], function () {
     Route::get('/search/chapter/{bookId}', 'SearchController@searchChapter');
     Route::get('/search/entity/siblings', 'SearchController@searchSiblings');
 
+    Route::get('/templates', 'PageTemplateController@list');
+    Route::get('/templates/{templateId}', 'PageTemplateController@get');
+
     // Other Pages
     Route::get('/', 'HomeController@index');
     Route::get('/home', 'HomeController@index');
diff --git a/tests/Entity/PageTemplateTest.php b/tests/Entity/PageTemplateTest.php
new file mode 100644 (file)
index 0000000..883de4a
--- /dev/null
@@ -0,0 +1,90 @@
+<?php namespace Entity;
+
+use BookStack\Entities\Page;
+use Tests\TestCase;
+
+class PageTemplateTest extends TestCase
+{
+    public function test_active_templates_visible_on_page_view()
+    {
+        $page = Page::first();
+
+        $this->asEditor();
+        $templateView = $this->get($page->getUrl());
+        $templateView->assertDontSee('Page Template');
+
+        $page->template = true;
+        $page->save();
+
+        $templateView = $this->get($page->getUrl());
+        $templateView->assertSee('Page Template');
+    }
+
+    public function test_manage_templates_permission_required_to_change_page_template_status()
+    {
+        $page = Page::first();
+        $editor = $this->getEditor();
+        $this->actingAs($editor);
+
+        $pageUpdateData = [
+            'name' => $page->name,
+            'html' => $page->html,
+            'template' => 'true',
+        ];
+
+        $this->put($page->getUrl(), $pageUpdateData);
+        $this->assertDatabaseHas('pages', [
+            'id' => $page->id,
+            'template' => false,
+        ]);
+
+        $this->giveUserPermissions($editor, ['templates-manage']);
+
+        $this->put($page->getUrl(), $pageUpdateData);
+        $this->assertDatabaseHas('pages', [
+            'id' => $page->id,
+            'template' => true,
+        ]);
+    }
+
+    public function test_templates_content_should_be_fetchable_only_if_page_marked_as_template()
+    {
+        $content = '<div>my_custom_template_content</div>';
+        $page = Page::first();
+        $editor = $this->getEditor();
+        $this->actingAs($editor);
+
+        $templateFetch = $this->get('/templates/' . $page->id);
+        $templateFetch->assertStatus(404);
+
+        $page->html = $content;
+        $page->template = true;
+        $page->save();
+
+        $templateFetch = $this->get('/templates/' . $page->id);
+        $templateFetch->assertStatus(200);
+        $templateFetch->assertJson([
+            'html' => $content,
+            'markdown' => '',
+        ]);
+    }
+
+    public function test_template_endpoint_returns_paginated_list_of_templates()
+    {
+        $editor = $this->getEditor();
+        $this->actingAs($editor);
+
+        $toBeTemplates = Page::query()->orderBy('name', 'asc')->take(12)->get();
+        $page = $toBeTemplates->first();
+
+        $emptyTemplatesFetch = $this->get('/templates');
+        $emptyTemplatesFetch->assertDontSee($page->name);
+
+        Page::query()->whereIn('id', $toBeTemplates->pluck('id')->toArray())->update(['template' => true]);
+
+        $templatesFetch = $this->get('/templates');
+        $templatesFetch->assertSee($page->name);
+        $templatesFetch->assertSee('pagination');
+    }
+
+}
\ No newline at end of file