From 23e014cb25263aa20f21edd343704c88a3131fc6 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Fri, 2 Sep 2016 18:54:26 +0100 Subject: [PATCH] Added link selector to markdown editor --- resources/assets/js/directives.js | 30 +++++++++++++++++-- resources/assets/js/global.js | 5 ++++ resources/views/pages/edit.blade.php | 17 ++--------- resources/views/pages/form.blade.php | 2 ++ .../partials/entity-selector-popup.blade.php | 14 +++++++++ 5 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 resources/views/partials/entity-selector-popup.blade.php diff --git a/resources/assets/js/directives.js b/resources/assets/js/directives.js index 94e281512..4e1bb0dfd 100644 --- a/resources/assets/js/directives.js +++ b/resources/assets/js/directives.js @@ -271,8 +271,6 @@ module.exports = function (ngApp, events) { scope.mdModel = content; scope.mdChange(markdown(content)); - console.log('test'); - element.on('change input', (event) => { content = element.val(); $timeout(() => { @@ -304,6 +302,7 @@ module.exports = function (ngApp, events) { const input = element.find('[markdown-input] textarea').first(); const display = element.find('.markdown-display').first(); const insertImage = element.find('button[data-action="insertImage"]'); + const insertEntityLink = element.find('button[data-action="insertEntityLink"]') let currentCaretPos = 0; @@ -355,6 +354,13 @@ module.exports = function (ngApp, events) { input[0].selectionEnd = caretPos + ('![](http://'.length); return; } + + // Insert entity link shortcut + if (event.which === 75 && event.ctrlKey && event.shiftKey) { + showLinkSelector(); + return; + } + // Pass key presses to controller via event scope.$emit('editor-keydown', event); }); @@ -370,6 +376,26 @@ module.exports = function (ngApp, events) { }); }); + function showLinkSelector() { + window.showEntityLinkSelector((entity) => { + let selectionStart = currentCaretPos; + let selectionEnd = input[0].selectionEnd; + let textSelected = (selectionEnd !== selectionStart); + let currentContent = input.val(); + + if (textSelected) { + let selectedText = currentContent.substring(selectionStart, selectionEnd); + let linkText = `[${selectedText}](${entity.link})`; + input.val(currentContent.substring(0, selectionStart) + linkText + currentContent.substring(selectionEnd)); + } else { + let linkText = ` [${entity.name}](${entity.link}) `; + input.val(currentContent.substring(0, selectionStart) + linkText + currentContent.substring(selectionStart)) + } + input.change(); + }); + } + insertEntityLink.click(showLinkSelector); + } } }]); diff --git a/resources/assets/js/global.js b/resources/assets/js/global.js index eb2750965..ace0dcdd2 100644 --- a/resources/assets/js/global.js +++ b/resources/assets/js/global.js @@ -140,6 +140,11 @@ $(function () { $(this).fadeOut(240); }); + $('.markdown-display').on('click', 'a', function(event) { + event.preventDefault(); + window.open($(this).attr('href')); + }); + }); // Page specific items diff --git a/resources/views/pages/edit.blade.php b/resources/views/pages/edit.blade.php index ad569a327..58df580a5 100644 --- a/resources/views/pages/edit.blade.php +++ b/resources/views/pages/edit.blade.php @@ -19,22 +19,9 @@ + @include('partials/image-manager', ['imageType' => 'gallery', 'uploaded_to' => $page->id]) - -
-
- -
-
+ @include('partials/entity-selector-popup')