From f8a72342003647841c87283b491e310ccd102b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Norman=20Hauk=C3=A5s?= Date: Mon, 30 Nov 2015 22:44:04 +0100 Subject: [PATCH] Some refactoring done. Now including the text that was tagged. --- src/index.interfaces.ts | 1 + src/services/webpage.service.ts | 35 ++++++++++++++------------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/index.interfaces.ts b/src/index.interfaces.ts index dacde2f..739e221 100644 --- a/src/index.interfaces.ts +++ b/src/index.interfaces.ts @@ -31,6 +31,7 @@ module tagIt { userEmail: string; sense: ISense; context: string; + wordThatWasTagged: string; serializedSelectionRange: string; deserializedRange?: Range; } diff --git a/src/services/webpage.service.ts b/src/services/webpage.service.ts index 06453c9..08616ca 100644 --- a/src/services/webpage.service.ts +++ b/src/services/webpage.service.ts @@ -25,6 +25,8 @@ module tagIt { rangy.init(); } + //this allows the menu controller to bind to the service so that it may + //notify the menu. TODO refactor to use events on $rootScope instead. wireUpListener(callbackOnSelectFunc: (result: Object) => void, callbackOnDeSelectFunc: () => void) { var that = this; @@ -39,7 +41,7 @@ module tagIt { this.tagStorageService.deleteTagById(evt.target.parentElement.id); } else if (this.findSelectedText()) { - this.updateSavedSelection(); + updateSavedSelection(); callbackOnSelectFunc(joinLongWords(this.findSelectedText())); } else { callbackOnDeSelectFunc(); @@ -57,18 +59,16 @@ module tagIt { theSurroundingSpanElement.parentNode .replaceChild(theOriginalTextNode, theSurroundingSpanElement); } - } - - getClonedSelectionRange() { - return this.findSelection().getRangeAt(0).cloneRange(); - } - - findSelection() { - return window.getSelection(); + function updateSavedSelection() { + if (this.savedSelection) { + rangy.removeMarkers(this.savedSelection); + } + this.savedSelection = rangy.saveSelection(); + } } findSelectedText() { - var selectedText = this.findSelection().toString(); + var selectedText = window.getSelection().toString(); if (selectedText) { this.$log.debug('text that was selected: ' + selectedText); return selectedText; @@ -84,12 +84,14 @@ module tagIt { var serializedRange = rangy.serializeRange( range, true, document.getElementById('tagit-body')); var generatedUuid: string = uuid.v4(); + var parentElement = range.commonAncestorContainer; return { id: generatedUuid, userEmail: 'testEmail', sense: sense, - context: range.commonAncestorContainer.innerText, + wordThatWasTagged: this.findSelectedText(), + context: parentElement.innerText, serializedSelectionRange: serializedRange } }; @@ -116,7 +118,7 @@ module tagIt { //deserialize ranges _.map(tagsToLoad, deserializeRange); - + this.$log.debug('finished deserializing tags'); //sort tags by ascending so that they can be properly inserted @@ -136,7 +138,7 @@ module tagIt { this.$log.debug('finished adding tags to page'); window.getSelection().removeAllRanges(); - + function deserializeRange(tagToLoad: ISenseTag) { try { tagToLoad.deserializedRange = rangy.deserializeRange( @@ -153,13 +155,6 @@ module tagIt { } } - private updateSavedSelection() { - if (this.savedSelection) { - rangy.removeMarkers(this.savedSelection); - } - this.savedSelection = rangy.saveSelection(); - } - private surroundRangeWithSpan(sense: ISense, range: Range, uuid: string) { // add span around content var span: HTMLSpanElement = document.createElement('span');