Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MouseManagerRemote.js #919

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions src/extensions/default/bramble/lib/MouseManagerRemote.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, forin: true, maxerr: 50, regexp: true, bitwise: true */
/* global addEventListener, removeEventListener, sessionStorage */
(function(transport) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't move this line. You need to wrap the entire module in a function like this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed the code and made the indentation, can you check and get back to me please


var testSessionStorage = "testSessionStorage";
var noop = function() {};
var sessionStorage = {
getItem: noop,
setItem: noop
};

try
{
window.sessionStorage.setItem(testSessionStorage, testSessionStorage);
window.sessionStorage.removeItem(testSessionStorage);
sessionStorage = window.sessionStorage.bind(window);
}

catch(e)
{
console.warn("[Bramble] Session storage not accessible for MouseManager.");

(function(transport)
{
"use strict";

var SCROLL_KEY = "___bramble-preview-scrollTop::" + window.___brambleFilename;

var listening = false;

function sendHighlightInfo(e) {
function sendHighlightInfo(e)
{
var x = e.pageX - window.pageXOffset;
var y = e.pageY - window.pageYOffset;
var elem = document.elementFromPoint(x, y);
if(!elem) {
if(!elem)
{
return;
}

var ds = elem.dataset;
if(!(ds.brambleStartLine && ds.brambleStartCh && ds.brambleEndLine && ds.brambleEndCh)) {
return;
}
}

// If the element has a brackets-id, and this was a click,
// use that info to highlight it. Don't bother for mousemove.
Expand All @@ -29,68 +51,81 @@

transport.send("bramble-highlight-lines:" + ds.brambleStartLine + "," +
ds.brambleStartCh + "," + ds.brambleEndLine + "," + ds.brambleEndCh + bracketsId);
}
}

function shouldIgnoreElemEvent(e) {
function shouldIgnoreElemEvent(e)
{
// Ignore mouseout for any inner elements (just for entire window)
var from = e.relatedTarget || e.toElement;
if(!from || from.nodeName === "HTML") {
if(!from || from.nodeName === "HTML")
{
// Further filter things by only responding to events close to the left/top
if(e.clientX <= 32 || e.clientY <= 32) {
if(e.clientX <= 32 || e.clientY <= 32)
{
return false;
}
}

return true;
}

function startListener() {
if(listening) {
function startListener()
{
if(listening)
{
return;
}

addEventListener("mousemove", sendHighlightInfo, false);
listening = true;
}

function stopListener() {
if(!listening) {

function stopListener()
{
if(!listening)
{
return;
}

removeEventListener("mousemove", sendHighlightInfo, false);
listening = false;
}

addEventListener("load", function() {
addEventListener("load", function()
{
// Restore last scroll position for this session (if any)
document.body.scrollTop = sessionStorage.getItem(SCROLL_KEY)|0;

// Do not listen for mouse events relevant for the inspector
// if there is no livedoc
if(!transport) {
return;
}
}

startListener();

// If the user clicks on an element, stop inspecting with mousemove (pin editor)
addEventListener("click", function(e) {
addEventListener("click", function(e)
{
transport.send("bramble-inspector-disable");
stopListener();
sendHighlightInfo(e);
});

addEventListener("mouseout", function(e) {
if(!shouldIgnoreElemEvent(e)) {
addEventListener("mouseout", function(e)
{
if(!shouldIgnoreElemEvent(e))
{
// Start listening for user inspecting elements (again)
startListener();
return false;
}
});

// Remember last scroll position for this document
addEventListener("scroll", function() {
addEventListener("scroll", function()
{
sessionStorage.setItem(SCROLL_KEY, document.body.scrollTop);
}, false);

Expand Down