Wyatt Allen f0eb4bbf53 Harden gr-formatted-text agains slow/failed project configs
Formerly, if a formatted text component tried to render without the
project config (used by inner linked text components) it would
temporarily fall-back to rendering the unformatted (and un-linkified)
text via `.textContent` -- mirroring the behavior of gr-linked-text.

The result is formatted text elements (when rendered without a project
config) appear as one long line of text. Unlike linkification, however,
text can be accurately formatted with or without the project config --
so this disruptive, poor UX is unnecessary.

The formatted text component is updated to format text when the project
config has not provided, and to re-render when the config has been
provided.

In order to propagate project config loads to the formatted text
components hosted by diff comments, the REST calls must be made by the
diff thread component. To make this call, the thread must have the
project's name, so gr-diff-builder is updated to provide this name to
thread components.

Bug: Issue 6686
Change-Id: I8d09c740930500e99cb5f87b92f4d72f3f50a9ce
2017-08-01 16:14:45 -07:00

151 lines
4.8 KiB
JavaScript

// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
(function(window, GrDiffBuilderSideBySide) {
'use strict';
// Prevent redefinition.
if (window.GrDiffBuilderImage) { return; }
const IMAGE_MIME_PATTERN = /^image\/(bmp|gif|jpeg|jpg|png|tiff|webp)$/;
function GrDiffBuilderImage(
diff, comments, prefs, projectName, outputEl, baseImage, revisionImage) {
GrDiffBuilderSideBySide.call(
this, diff, comments, prefs, projectName, outputEl, []);
this._baseImage = baseImage;
this._revisionImage = revisionImage;
}
GrDiffBuilderImage.prototype = Object.create(
GrDiffBuilderSideBySide.prototype);
GrDiffBuilderImage.prototype.constructor = GrDiffBuilderImage;
GrDiffBuilderImage.prototype.renderDiffImages = function() {
const section = this._createElement('tbody', 'image-diff');
this._emitImagePair(section);
this._emitImageLabels(section);
this._outputEl.appendChild(section);
};
GrDiffBuilderImage.prototype._emitImagePair = function(section) {
const tr = this._createElement('tr');
tr.appendChild(this._createElement('td'));
tr.appendChild(this._createImageCell(this._baseImage, 'left', section));
tr.appendChild(this._createElement('td'));
tr.appendChild(this._createImageCell(
this._revisionImage, 'right', section));
section.appendChild(tr);
};
GrDiffBuilderImage.prototype._createImageCell = function(image, className,
section) {
const td = this._createElement('td', className);
if (image && IMAGE_MIME_PATTERN.test(image.type)) {
const imageEl = this._createElement('img');
imageEl.onload = function() {
image._height = imageEl.naturalHeight;
image._width = imageEl.naturalWidth;
this._updateImageLabel(section, className, image);
}.bind(this);
imageEl.src = 'data:' + image.type + ';base64, ' + image.body;
imageEl.addEventListener('error', () => {
imageEl.remove();
td.textContent = '[Image failed to load]';
});
td.appendChild(imageEl);
}
return td;
};
GrDiffBuilderImage.prototype._updateImageLabel = function(section, className,
image) {
const label = Polymer.dom(section)
.querySelector('.' + className + ' span.label');
this._setLabelText(label, image);
};
GrDiffBuilderImage.prototype._setLabelText = function(label, image) {
label.textContent = this._getImageLabel(image);
};
GrDiffBuilderImage.prototype._emitImageLabels = function(section) {
const tr = this._createElement('tr');
let addNamesInLabel = false;
if (this._baseImage && this._revisionImage &&
this._baseImage._name !== this._revisionImage._name) {
addNamesInLabel = true;
}
tr.appendChild(this._createElement('td'));
let td = this._createElement('td', 'left');
let label = this._createElement('label');
let nameSpan;
let labelSpan = this._createElement('span', 'label');
if (addNamesInLabel) {
nameSpan = this._createElement('span', 'name');
nameSpan.textContent = this._baseImage._name;
label.appendChild(nameSpan);
label.appendChild(this._createElement('br'));
}
this._setLabelText(labelSpan, this._baseImage, addNamesInLabel);
label.appendChild(labelSpan);
td.appendChild(label);
tr.appendChild(td);
tr.appendChild(this._createElement('td'));
td = this._createElement('td', 'right');
label = this._createElement('label');
labelSpan = this._createElement('span', 'label');
if (addNamesInLabel) {
nameSpan = this._createElement('span', 'name');
nameSpan.textContent = this._revisionImage._name;
label.appendChild(nameSpan);
label.appendChild(this._createElement('br'));
}
this._setLabelText(labelSpan, this._revisionImage, addNamesInLabel);
label.appendChild(labelSpan);
td.appendChild(label);
tr.appendChild(td);
section.appendChild(tr);
};
GrDiffBuilderImage.prototype._getImageLabel = function(image) {
if (image) {
const type = image.type || image._expectedType;
if (image._width && image._height) {
return image._width + '⨉' + image._height + ' ' + type;
} else {
return type;
}
}
return 'No image';
};
window.GrDiffBuilderImage = GrDiffBuilderImage;
})(window, GrDiffBuilderSideBySide);