From c0febab83758b7eecd1afeeba3a03a88e781a9cf Mon Sep 17 00:00:00 2001 From: Juanfran Date: Fri, 28 Nov 2014 09:00:04 +0100 Subject: [PATCH] fix #1710 select text in the wysiwyng preview actives edition mode --- app/coffee/modules/common.coffee | 15 +++++++++++++++ app/coffee/modules/common/components.coffee | 15 ++++----------- app/coffee/modules/common/wisiwyg.coffee | 16 +++++++++++----- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/coffee/modules/common.coffee b/app/coffee/modules/common.coffee index 594469a8..b9b31365 100644 --- a/app/coffee/modules/common.coffee +++ b/app/coffee/modules/common.coffee @@ -23,6 +23,21 @@ taiga = @.taiga module = angular.module("taigaCommon", []) +############################################################################# +## Get the selected text +############################################################################# +SelectedText = ($window, $document) -> + get = () -> + if $window.getSelection + return $window.getSelection().toString() + else if $document.selection + return $document.selection.createRange().text + return "" + + return {get: get} + +module.factory("$selectedText", ["$window", "$document", SelectedText]) + ############################################################################# ## Permission directive, hide elements when necessary ############################################################################# diff --git a/app/coffee/modules/common/components.coffee b/app/coffee/modules/common/components.coffee index 6a7057fe..f4a4b419 100644 --- a/app/coffee/modules/common/components.coffee +++ b/app/coffee/modules/common/components.coffee @@ -553,7 +553,7 @@ module.directive("tgEditableSubject", ["$rootScope", "$tgRepo", "$tgConfirm", "$ ## Editable subject directive ############################################################################# -EditableDescriptionDirective = ($window, $document, $rootscope, $repo, $confirm, $compile, $loading) -> +EditableDescriptionDirective = ($rootscope, $repo, $confirm, $compile, $loading, $selectedText) -> template = """
return $scope.project.my_permissions.indexOf($attrs.requiredPerm) != -1 - getSelectedText = -> - if $window.getSelection - return $window.getSelection().toString() - else if $document.selection - return $document.selection.createRange().text - return null - $el.on "mouseup", ".view-description", (event) -> # We want to dettect the a inside the div so we use the target and # not the currentTarget target = angular.element(event.target) return if not isEditable() return if target.is('a') - return if getSelectedText() + return if $selectedText.get().length $el.find('.edit-description').show() $el.find('.view-description').hide() @@ -654,8 +647,8 @@ EditableDescriptionDirective = ($window, $document, $rootscope, $repo, $confirm, template: template } -module.directive("tgEditableDescription", ["$window", "$document", "$rootScope", "$tgRepo", "$tgConfirm", - "$compile", "$tgLoading", EditableDescriptionDirective]) +module.directive("tgEditableDescription", ["$rootScope", "$tgRepo", "$tgConfirm", + "$compile", "$tgLoading", "$selectedText", EditableDescriptionDirective]) ############################################################################# diff --git a/app/coffee/modules/common/wisiwyg.coffee b/app/coffee/modules/common/wisiwyg.coffee index cb0f1443..e1ceeae2 100644 --- a/app/coffee/modules/common/wisiwyg.coffee +++ b/app/coffee/modules/common/wisiwyg.coffee @@ -28,7 +28,7 @@ module = angular.module("taigaCommon") ############################################################################# ## WYSIWYG markitup editor directive ############################################################################# -tgMarkitupDirective = ($rootscope, $rs, $tr) -> +tgMarkitupDirective = ($rootscope, $rs, $tr, $selectedText) -> previewTemplate = _.template("""
@@ -61,10 +61,16 @@ tgMarkitupDirective = ($rootscope, $rs, $tr) -> markdownDomNode.append(previewTemplate({data: data.data})) markItUpDomNode.hide() - # FIXME: Really `.parents()` is need? seems `.closest` - # function is better aproach for it - element.parents(".markdown").one "click", ".preview", (event) -> + markdown = element.closest(".markdown") + + markdown.on "mouseup.preview", ".preview", (event) -> event.preventDefault() + target = angular.element(event.target) + + if !target.is('a') and $selectedText.get().length + return + + markdown.off(".preview") closePreviewMode() markdownCaretPositon = false @@ -277,4 +283,4 @@ tgMarkitupDirective = ($rootscope, $rs, $tr) -> return {link:link, require:"ngModel"} -module.directive("tgMarkitup", ["$rootScope", "$tgResources", "$tgI18n", tgMarkitupDirective]) +module.directive("tgMarkitup", ["$rootScope", "$tgResources", "$tgI18n", "$selectedText", tgMarkitupDirective])