Skip to content

Commit

Permalink
javascript/textareas.js: re-factor message handling
Browse files Browse the repository at this point in the history
Start every message with a template from getEmptyMessage() and fill in
the details. Include the document URL in the message to avoid needing
tab info.
  • Loading branch information
stsquad committed Mar 16, 2019
1 parent 089cb98 commit 01d84d2
Showing 1 changed file with 57 additions and 38 deletions.
95 changes: 57 additions & 38 deletions javascript/textareas.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ function getTitle()
return title;
}

/*
getEmptyMessage
Return a partially filled message hash with details.
*/
function getEmptyMessage()
{
var empty_msg = {
pageUrl: document.URL,
title: getTitle(),
};
return empty_msg;
}
/*
getEditButton
Expand Down Expand Up @@ -240,49 +253,56 @@ function updateTextArea(id, content) {
sendTextArea
Send the text area to the main part of the extension to be passed
on to the external editor. Eventually
on to the external editor. Eventually
*/

function sendTextArea(text_tracker) {
// And spawn the request
var edit_msg = {
msg: "edit",
text: text_tracker.getContent(),
title: getTitle(),
id: text_tracker.edit_id
};
var edit_msg = getEmptyMessage();
edit_msg.msg = "edit";
edit_msg.text = text_tracker.getContent();
edit_msg.id = text_tracker.edit_id;
port.postMessage(edit_msg);
}

/*
Handle focused text area
*/
(function(){
var focusedEdit = null;

findActiveTextArea = function() {
if (focusedEdit) {
sendTextArea(focusedEdit);
} else {
var msg_text = "No textarea in focus in: "+getTitle();
port.postMessage( {msg: "error", orig_cmd: "find_edit", text: msg_text} );
}
};

setFocused = function(){
// Update UI?
var id = this.getAttribute("edit_id");
focusedEdit = getTextAreaTracker(id);
if (focusedEdit !== undefined) {
port.postMessage( {msg: "focus", id: id} );
this.addEventListener('blur', function() {
port.postMessage( {msg: "focus", id: null} );
this.removeEventListener('blur',arguments.callee,false);
});
} else {
console.log ("setFocused: failed to find a tracker for "+id);
var focusedEdit = null;

findActiveTextArea = function() {
if (focusedEdit) {
sendTextArea(focusedEdit);
} else {
var no_txt_msg = getEmptyMessage();
no_txt_msg.msg = "error";
no_txt_msg.text = "No textarea in focus in: "+getTitle();
no_txt_msg.orig_cmd = "find_edit";
port.postMessage(no_txt_msg);
}
};
};

setFocused = function(){
// Update UI?
var id = this.getAttribute("edit_id");
focusedEdit = getTextAreaTracker(id);
if (focusedEdit !== undefined) {
var focus_msg = getEmptyMessage();
focus_msg.msg = "focus";
focus_msg.id = id;
port.postMessage( focus_msg );
this.addEventListener('blur', function() {
var unfocus_msg = getEmptyMessage();
unfocus_msg.msg = "focus";
unfocus_msg.id = null;
port.postMessage( unfocus_msg );
this.removeEventListener('blur',arguments.callee,false);
});
} else {
console.log ("setFocused: failed to find a tracker for "+id);
}
};
})();

/*
Expand Down Expand Up @@ -388,7 +408,7 @@ function localMessageHandler(msg, port) {
enable_debug = msg.enable_debug;
findTextAreas([$('*')]);

/*
/*
* The mutation summary is responsible for monitoring all
* changes to the page and triggering updates.
*
Expand Down Expand Up @@ -442,14 +462,13 @@ port.postMessage({msg: "config"});
document.addEventListener("contextmenu", (function(event) {
var elem = event.srcElement;
if (elem && elem.getAttribute("edit_id")) {
var edit_msg = getEmptyMessage();
edit_msg.msg = "edit";
edit_msg.text = elem.value;
edit_msg.id = elem.getAttribute("edit_id");
var request = {
type: "menu_target",
edit_msg: {
msg: "edit",
text: elem.value,
title: getTitle(),
id: elem.getAttribute("edit_id")
}
edit_msg: edit_msg
};
browser_sendMessage(request);
}
Expand Down

0 comments on commit 01d84d2

Please sign in to comment.