Skip to content

Commit 5447a95

Browse files
committed
feat: add support for ruler lines pixel values
1 parent d973d83 commit 5447a95

File tree

1 file changed

+105
-87
lines changed

1 file changed

+105
-87
lines changed

src/LiveDevelopment/BrowserScripts/RemoteFunctions.js

Lines changed: 105 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4280,118 +4280,136 @@ function RemoteFunctions(config = {}) {
42804280
*/
42814281
function RulerLines(element) {
42824282
this.element = element;
4283-
this.lineElements = {
4284-
left: null,
4285-
right: null,
4286-
top: null,
4287-
bottom: null
4288-
};
4289-
// gray color for non-editable elements, blue for editable
4290-
this.color = element.hasAttribute("data-brackets-id")
4283+
4284+
const editable = element.hasAttribute("data-brackets-id");
4285+
this.color = editable
42914286
? "rgba(66, 133, 244, 0.4)"
42924287
: "rgba(60, 63, 65, 0.8)";
4288+
this.labelColor = editable
4289+
? "rgba(66, 133, 244, 1)"
4290+
: "rgba(60, 63, 65, 1)";
4291+
4292+
this.sides = ["left", "right", "top", "bottom"];
4293+
this.lineElements = {};
4294+
this.labelElements = {};
4295+
42934296
this.create();
42944297
this.update();
42954298
}
42964299

42974300
RulerLines.prototype = {
4298-
create: function() {
4299-
let body = window.document.body;
4300-
4301-
this.lineElements.left = window.document.createElement("div");
4302-
this.lineElements.right = window.document.createElement("div");
4303-
this.lineElements.top = window.document.createElement("div");
4304-
this.lineElements.bottom = window.document.createElement("div");
4305-
4306-
this.lineElements.left.setAttribute("data-phcode-internal-c15r5a9", "true");
4307-
this.lineElements.right.setAttribute("data-phcode-internal-c15r5a9", "true");
4308-
this.lineElements.top.setAttribute("data-phcode-internal-c15r5a9", "true");
4309-
this.lineElements.bottom.setAttribute("data-phcode-internal-c15r5a9", "true");
4310-
4311-
let applyStyles = function (element, color) {
4312-
element.style.position = "absolute";
4313-
element.style.backgroundColor = color;
4314-
element.style.pointerEvents = "none";
4315-
element.style.zIndex = "2147483645";
4301+
create() {
4302+
const body = document.body;
4303+
4304+
const makeDiv = () => {
4305+
const el = document.createElement("div");
4306+
el.setAttribute("data-phcode-internal-c15r5a9", "true");
4307+
return el;
43164308
};
43174309

4318-
applyStyles(this.lineElements.left, this.color);
4319-
applyStyles(this.lineElements.right, this.color);
4320-
applyStyles(this.lineElements.top, this.color);
4321-
applyStyles(this.lineElements.bottom, this.color);
4310+
const styleLine = (el) => {
4311+
el.style.position = "absolute";
4312+
el.style.backgroundColor = this.color;
4313+
el.style.pointerEvents = "none";
4314+
el.style.zIndex = "2147483645";
4315+
};
43224316

4323-
body.appendChild(this.lineElements.left);
4324-
body.appendChild(this.lineElements.right);
4325-
body.appendChild(this.lineElements.top);
4326-
body.appendChild(this.lineElements.bottom);
4327-
},
4317+
const styleLabel = (el) => {
4318+
el.style.position = "absolute";
4319+
el.style.color = this.labelColor;
4320+
el.style.fontSize = "9px";
4321+
el.style.fontFamily = "Arial, sans-serif";
4322+
el.style.pointerEvents = "none";
4323+
el.style.zIndex = "2147483646";
4324+
el.style.whiteSpace = "nowrap";
4325+
el.style.backgroundColor = "transparent";
4326+
};
43284327

4329-
update: function() {
4330-
if (!this.element) {
4331-
return;
4328+
for (const side of this.sides) {
4329+
const line = makeDiv();
4330+
const label = makeDiv();
4331+
4332+
styleLine(line);
4333+
styleLabel(label);
4334+
4335+
this.lineElements[side] = line;
4336+
this.labelElements[side] = label;
4337+
4338+
body.appendChild(line);
4339+
body.appendChild(label);
43324340
}
4341+
},
43334342

4334-
let rect = this.element.getBoundingClientRect();
4335-
let scrollTop = window.pageYOffset;
4336-
let scrollLeft = window.pageXOffset;
4343+
update() {
4344+
if (!this.element) { return; }
43374345

4338-
var edges = {
4339-
// 0.8 is to fix pixel diff between ruler and outline (ruler is part of box model but outline is not)
4346+
const rect = this.element.getBoundingClientRect();
4347+
const scrollTop = window.pageYOffset;
4348+
const scrollLeft = window.pageXOffset;
4349+
4350+
const edges = {
43404351
left: rect.left + scrollLeft - 0.8,
43414352
right: rect.right + scrollLeft,
43424353
top: rect.top + scrollTop - 0.8,
43434354
bottom: rect.bottom + scrollTop
43444355
};
43454356

4346-
// get the doc dimensions as we need to put the ruler lines in the whole document
4347-
var docHeight = window.document.documentElement.scrollHeight;
4348-
var docWidth = window.document.documentElement.scrollWidth;
4349-
4350-
// for vertical lines
4351-
this.lineElements.left.style.width = '1px';
4352-
this.lineElements.left.style.height = docHeight + 'px';
4353-
this.lineElements.left.style.left = edges.left + 'px';
4354-
this.lineElements.left.style.top = '0px';
4355-
4356-
this.lineElements.right.style.width = '1px';
4357-
this.lineElements.right.style.height = docHeight + 'px';
4358-
this.lineElements.right.style.left = edges.right + 'px';
4359-
this.lineElements.right.style.top = '0px';
4360-
4361-
// for horizontal lines
4362-
this.lineElements.top.style.height = '1px';
4363-
this.lineElements.top.style.width = docWidth + 'px';
4364-
this.lineElements.top.style.top = edges.top + 'px';
4365-
this.lineElements.top.style.left = '0px';
4366-
4367-
this.lineElements.bottom.style.height = '1px';
4368-
this.lineElements.bottom.style.width = docWidth + 'px';
4369-
this.lineElements.bottom.style.top = edges.bottom + 'px';
4370-
this.lineElements.bottom.style.left = '0px';
4357+
const docHeight = document.documentElement.scrollHeight;
4358+
const docWidth = document.documentElement.scrollWidth;
4359+
4360+
// Position lines
4361+
this.lineElements.left.style.cssText += `
4362+
width:1px; height:${docHeight}px;
4363+
left:${edges.left}px; top:0px;
4364+
`;
4365+
this.lineElements.right.style.cssText += `
4366+
width:1px; height:${docHeight}px;
4367+
left:${edges.right}px; top:0px;
4368+
`;
4369+
this.lineElements.top.style.cssText += `
4370+
height:1px; width:${docWidth}px;
4371+
top:${edges.top}px; left:0px;
4372+
`;
4373+
this.lineElements.bottom.style.cssText += `
4374+
height:1px; width:${docWidth}px;
4375+
top:${edges.bottom}px; left:0px;
4376+
`;
4377+
4378+
const x1 = Math.floor(edges.left + 1);
4379+
const x2 = x1 + rect.width;
4380+
const y1 = Math.floor(edges.top + 1);
4381+
const y2 = y1 + rect.height;
4382+
4383+
this.labelElements.left.textContent = x1 + "px";
4384+
this.labelElements.right.textContent = x2 + "px";
4385+
this.labelElements.top.textContent = y1 + "px";
4386+
this.labelElements.bottom.textContent = y2 + "px";
4387+
4388+
this.labelElements.left.style.left = edges.left + "px";
4389+
this.labelElements.right.style.left = edges.right + "px";
4390+
this.labelElements.top.style.left = (scrollLeft + 10) + "px";
4391+
this.labelElements.bottom.style.left = (scrollLeft + 10) + "px";
4392+
4393+
this.labelElements.left.style.top = (scrollTop + 10) + "px";
4394+
this.labelElements.right.style.top = (scrollTop + 10) + "px";
4395+
4396+
this.labelElements.top.style.top = edges.top + "px";
4397+
this.labelElements.bottom.style.top = edges.bottom + "px";
43714398
},
43724399

4373-
remove: function() {
4374-
var body = window.document.body;
4400+
remove() {
4401+
const body = document.body;
43754402

4376-
if (this.lineElements.left && this.lineElements.left.parentNode) {
4377-
body.removeChild(this.lineElements.left);
4378-
}
4379-
if (this.lineElements.right && this.lineElements.right.parentNode) {
4380-
body.removeChild(this.lineElements.right);
4381-
}
4382-
if (this.lineElements.top && this.lineElements.top.parentNode) {
4383-
body.removeChild(this.lineElements.top);
4384-
}
4385-
if (this.lineElements.bottom && this.lineElements.bottom.parentNode) {
4386-
body.removeChild(this.lineElements.bottom);
4403+
for (const side of this.sides) {
4404+
const line = this.lineElements[side];
4405+
const label = this.labelElements[side];
4406+
4407+
if (line && line.parentNode) { body.removeChild(line); }
4408+
if (label && label.parentNode) { body.removeChild(label); }
43874409
}
43884410

4389-
this.lineElements = {
4390-
left: null,
4391-
right: null,
4392-
top: null,
4393-
bottom: null
4394-
};
4411+
this.lineElements = {};
4412+
this.labelElements = {};
43954413
}
43964414
};
43974415

0 commit comments

Comments
 (0)