Skip to content

Commit

Permalink
fix: use dom builder for gutter tooltip and inline widget (#5601)
Browse files Browse the repository at this point in the history
* fix: use dom builder for gutter tooltip and inline widget
  • Loading branch information
akoreman committed Jun 27, 2024
1 parent e78b1ac commit e81a299
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/autocomplete/inline_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ module.exports = {
inline.show(editor, completions[3], "f");
editor.renderer.$loop._flush();
assert.strictEqual(getAllLines(), textBase + "function foo() {");
assert.strictEqual(editor.renderer.$ghostTextWidget.html, "<div> console.log('test');</div><div> }</div>");
assert.strictEqual(editor.renderer.$ghostTextWidget.el.innerHTML, "<div> console.log('test');</div><div> }</div>");
assert.strictEqual(editor.renderer.$ghostTextWidget.el.innerHTML, `<div> console.log('test');</div><div> }</div>`);
done();
},
"test: boundary tests": function(done) {
Expand Down
4 changes: 2 additions & 2 deletions src/ext/inline_autocomplete_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var getAllLines = function() {
return node.textContent;
}).join("\n");
if (editor.renderer.$ghostTextWidget) {
return text + "\n" + editor.renderer.$ghostTextWidget.html;
return text + "\n" + editor.renderer.$ghostTextWidget.el.innerHTML;
}
return text;
};
Expand Down Expand Up @@ -358,7 +358,7 @@ module.exports = {
typeAndChange("u", "n");
editor.renderer.$loop._flush();
assert.strictEqual(autocomplete.isOpen(), true);
assert.equal(getAllLines(), "function foo() {\n<div> console.log('test');</div><div>}</div>");
assert.equal(getAllLines(), `function foo() {\n<div> console.log('test');</div><div>}</div>`);

typeAndChange("d");
editor.renderer.$loop._flush();
Expand Down
31 changes: 25 additions & 6 deletions src/mouse/default_gutter_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,32 @@ class GutterTooltip extends Tooltip {

// Construct the contents of the tooltip.
for (let i = 0; i < annotation.text.length; i++) {
var line = `<span class='ace_${annotation.type[i]} ${iconClassName}' aria-label='${GutterTooltip.annotationLabels[annotation.type[i].replace("_fold","")].singular}' role=img> </span> ${annotation.text[i]}`;
annotationMessages[annotation.type[i].replace("_fold","")].push(line);
var lineElement = dom.createElement("span");

var iconElement = dom.createElement("span");
iconElement.classList.add(...[`ace_${annotation.type[i]}`, iconClassName]);
iconElement.setAttribute("aria-label", `${GutterTooltip.annotationLabels[annotation.type[i].replace("_fold","")].singular}`);
iconElement.setAttribute("role", "img");
// Set empty content to the img span to get it to show up
iconElement.appendChild(dom.createTextNode(" "));

lineElement.appendChild(iconElement);
lineElement.appendChild(dom.createTextNode(`${annotation.text[i]}`));
lineElement.appendChild(dom.createElement("br"));

annotationMessages[annotation.type[i].replace("_fold","")].push(lineElement);
}
var tooltipContent = [].concat(annotationMessages.error, annotationMessages.warning, annotationMessages.info).join("<br>");

this.setHtml(tooltipContent);
this.$element.setAttribute("aria-live", "polite");

// Clear the current tooltip content
var tooltipElement = this.getElement();
dom.removeChildren(tooltipElement);

// Update the tooltip content
annotationMessages.error.forEach(el => tooltipElement.appendChild(el));
annotationMessages.warning.forEach(el => tooltipElement.appendChild(el));
annotationMessages.info.forEach(el => tooltipElement.appendChild(el));

tooltipElement.setAttribute("aria-live", "polite");

if (!this.isOpen) {
this.setTheme(this.editor.renderer.theme);
Expand Down
14 changes: 11 additions & 3 deletions src/virtual_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1776,13 +1776,21 @@ class VirtualRenderer {
column: insertPosition. column
}
};

var widgetDiv = dom.createElement("div");
if (textChunks.length > 1) {
var divs = textChunks.slice(1).map(el => {
return `<div${el.wrapped ? ' class="ghost_text_line_wrapped"': ""}>${el.text}</div>`;
textChunks.slice(1).forEach(el => {
var chunkDiv = dom.createElement("div");

// If the line is wider than the viewport, wrap the line
if (el.wrapped) chunkDiv.className = "ghost_text_line_wrapped";

chunkDiv.appendChild(dom.createTextNode(el.text));
widgetDiv.appendChild(chunkDiv);
});

this.$ghostTextWidget = {
html: divs.join(""),
el: widgetDiv,
row: insertPosition.row,
column: insertPosition.column,
className: "ace_ghost_text"
Expand Down
4 changes: 2 additions & 2 deletions src/virtual_renderer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ module.exports = {
editor.renderer.$loop._flush();
assert.equal(editor.renderer.content.textContent, "abcdefGhost1");

assert.equal(editor.session.lineWidgets[0].el.innerHTML, "<div>Ghost2</div><div>Ghost3</div>");
assert.equal(editor.session.lineWidgets[0].el.innerHTML, `<div>Ghost2</div><div>Ghost3</div>`);

editor.removeGhostText();

Expand All @@ -395,7 +395,7 @@ module.exports = {
editor.renderer.$loop._flush();
assert.equal(editor.renderer.content.textContent, "abcdefThis is a long test text that is longer than ");

assert.equal(editor.session.lineWidgets[0].el.innerHTML, "<div class=\"ghost_text_line_wrapped\">30 characters</div><div></div><div>Ghost3</div>");
assert.equal(editor.session.lineWidgets[0].el.innerHTML, `<div class="ghost_text_line_wrapped">30 characters</div><div></div><div>Ghost3</div>`);

editor.removeGhostText();

Expand Down

0 comments on commit e81a299

Please sign in to comment.