@difizen/mana-app
Version:
493 lines (482 loc) • 20.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.URIIconReference = exports.LabelProviderContribution = exports.LabelProvider = exports.DefaultUriLabelProviderContribution = void 0;
var _manaCommon = require("@difizen/mana-common");
var _manaCore = require("@difizen/mana-core");
var _manaSyringe = require("@difizen/mana-syringe");
var _fileIconsJs = _interopRequireDefault(require("file-icons-js"));
var _dec, _dec2, _class, _class2, _dec3, _class3;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
/**
* @internal don't export it, use `LabelProvider.folderIcon` instead.
*/
var DEFAULT_FOLDER_ICON = 'fa fa-folder default-folder-icon';
/**
* @internal don't export it, use `LabelProvider.fileIcon` instead.
*/
var DEFAULT_FILE_ICON = 'fa fa-file default-file-icon';
var LabelProviderContribution = exports.LabelProviderContribution = _manaSyringe.Syringe.defineToken('LabelProviderContribution');
/**
* A {@link LabelProviderContribution} determines how specific elements/nodes are displayed in the workbench.
* Mana views use a common {@link LabelProvider} to determine the label and/or an icon for elements shown in the UI. This includes elements in lists
* and trees, but also view specific locations like headers. The common {@link LabelProvider} collects all {@links LabelProviderContribution} and delegates
* to the contribution with the highest priority. This is determined via calling the {@link LabelProviderContribution.canHandle} function, so contributions
* define which elements they are responsible for.
* As arbitrary views can consume LabelProviderContributions, they must be generic for the covered element type, not view specific. Label providers and
* contributions can be used for arbitrary element and node types, e.g. for markers or domain-specific elements.
*/
var URIIconReference;
(function (_URIIconReference) {
function is(element) {
return !!element && _typeof(element) === 'object' && 'kind' in element && element.kind === 'uriIconReference';
}
_URIIconReference.is = is;
function create(id, uri) {
return {
kind: 'uriIconReference',
id: id,
uri: uri
};
}
_URIIconReference.create = create;
})(URIIconReference || (exports.URIIconReference = URIIconReference = {}));
var DefaultUriLabelProviderContribution = exports.DefaultUriLabelProviderContribution = (_dec = (0, _manaSyringe.singleton)({
contrib: LabelProviderContribution
}), _dec2 = (0, _manaSyringe.postConstruct)(), _dec(_class = (_class2 = /*#__PURE__*/function () {
function DefaultUriLabelProviderContribution() {
_classCallCheck(this, DefaultUriLabelProviderContribution);
this.formatters = [];
this.onDidChangeEmitter = new _manaCommon.Emitter();
this.homePath = '/';
// copied and modified from https://github.com/microsoft/vscode/blob/1.44.2/src/vs/workbench/services/label/common/labelService.ts
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
this.labelMatchingRegexp = /\${(scheme|authority|path|query)}/g;
}
_createClass(DefaultUriLabelProviderContribution, [{
key: "init",
value: function init() {
//
}
}, {
key: "canHandle",
value: function canHandle(element) {
if (element instanceof _manaCommon.URI || URIIconReference.is(element)) {
return 1;
}
return 0;
}
}, {
key: "getIcon",
value: function getIcon(element) {
if (URIIconReference.is(element) && element.id === 'folder') {
return this.defaultFolderIcon;
}
var uri = URIIconReference.is(element) ? element.uri : element;
if (uri) {
var iconClass = uri && this.getFileIcon(uri);
return iconClass || this.defaultFileIcon;
}
return '';
}
}, {
key: "defaultFolderIcon",
get: function get() {
return DEFAULT_FOLDER_ICON;
}
}, {
key: "defaultFileIcon",
get: function get() {
return DEFAULT_FILE_ICON;
}
}, {
key: "getFileIcon",
value: function getFileIcon(uri) {
var fileIcon = _fileIconsJs.default.getClassWithColor(uri.displayName);
if (!fileIcon) {
return undefined;
}
return "".concat(fileIcon, " mana-file-icons-js");
}
}, {
key: "getName",
value: function getName(element) {
var uri = this.getUri(element);
return uri && uri.displayName;
}
}, {
key: "getLongName",
value: function getLongName(element) {
var uri = this.getUri(element);
if (uri) {
var formatting = this.findFormatting(uri);
if (formatting) {
return this.formatUri(uri, formatting);
}
}
return uri && uri.path.toString();
}
}, {
key: "getUri",
value: function getUri(element) {
return URIIconReference.is(element) ? element.uri : element;
}
}, {
key: "registerFormatter",
value: function registerFormatter(formatter) {
var _this = this;
this.formatters.push(formatter);
this.fireOnDidChange();
return _manaCommon.Disposable.create(function () {
_this.formatters = _this.formatters.filter(function (f) {
return f !== formatter;
});
_this.fireOnDidChange();
});
}
}, {
key: "onDidChange",
get: function get() {
return this.onDidChangeEmitter.event;
}
}, {
key: "fireOnDidChange",
value: function fireOnDidChange() {
var _this2 = this;
this.onDidChangeEmitter.fire({
affects: function affects(element) {
return _this2.canHandle(element) > 0;
}
});
}
}, {
key: "formatUri",
value: function formatUri(resource, formatting) {
var label = formatting.label.replace(this.labelMatchingRegexp, function (_match, token) {
switch (token) {
case 'scheme':
return resource.scheme;
case 'authority':
return resource.authority;
case 'path':
return resource.path.toString();
case 'query':
return resource.query;
default:
return '';
}
});
// convert \c:\something => C:\something
if (formatting.normalizeDriveLetter && this.hasDriveLetter(label)) {
label = label.charAt(1).toUpperCase() + label.substr(2);
}
if (formatting.authorityPrefix && resource.authority) {
label = formatting.authorityPrefix + label;
}
return label.replace(/\//g, formatting.separator);
}
}, {
key: "hasDriveLetter",
value: function hasDriveLetter(path) {
return !!(path && path[2] === ':');
}
}, {
key: "findFormatting",
value: function findFormatting(resource) {
var bestResult;
this.formatters.forEach(function (formatter) {
if (formatter.scheme === resource.scheme) {
if (!bestResult && !formatter.authority) {
bestResult = formatter;
return;
}
if (!formatter.authority) {
return;
}
if (formatter.authority.toLowerCase() === resource.authority.toLowerCase() && (!bestResult || !bestResult.authority || formatter.authority.length > bestResult.authority.length || formatter.authority.length === bestResult.authority.length && formatter.priority)) {
bestResult = formatter;
}
}
});
return bestResult ? bestResult.formatting : undefined;
}
}]);
return DefaultUriLabelProviderContribution;
}(), (_applyDecoratedDescriptor(_class2.prototype, "init", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "init"), _class2.prototype)), _class2)) || _class);
/**
* The {@link LabelProvider} determines how elements/nodes are displayed in the workbench. For any element, it can determine a short label, a long label
* and an icon. The {@link LabelProvider} is to be used in lists, trees and tables, but also view specific locations like headers.
* The common {@link LabelProvider} can be extended/adapted via {@link LabelProviderContribution}s. For every element, the {@links LabelProvider} will determine the
* {@link LabelProviderContribution} with the hightest priority and delegate to it. Mana registers default {@link LabelProviderContribution} for common types, e.g.
* the {@link DefaultUriLabelProviderContribution} for elements that have a URI.
* Using the {@link LabelProvider} across the workbench ensures a common look and feel for elements across multiple views. To adapt the way how specific
* elements/nodes are rendered, use a {@link LabelProviderContribution} rather than adapting or sub classing the {@link LabelProvider}. This way, your adaptation
* is applied to all views in Mana that use the {@link LabelProvider}
*/
var LabelProvider = exports.LabelProvider = (_dec3 = (0, _manaSyringe.singleton)({
contrib: _manaCore.ApplicationContribution
}), _dec3(_class3 = /*#__PURE__*/function () {
function LabelProvider(contributionProvider) {
_classCallCheck(this, LabelProvider);
this.onDidChangeEmitter = new _manaCommon.Emitter();
this.contributionProvider = contributionProvider;
}
/**
* Start listening to contributions.
*
* Don't call this method directly!
* It's called by the frontend application during initialization.
*/
LabelProvider = (0, _manaSyringe.contrib)(LabelProviderContribution)(LabelProvider, undefined, 0) || LabelProvider;
_createClass(LabelProvider, [{
key: "initialize",
value: function initialize() {
var _this3 = this;
var contributions = this.contributionProvider.getContributions();
var _iterator = _createForOfIteratorHelper(contributions),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var eventContribution = _step.value;
if (eventContribution.onDidChange) {
eventContribution.onDidChange(function (event) {
_this3.onDidChangeEmitter.fire({
affects: function affects(element) {
return _this3.affects(element, event);
}
});
});
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
}, {
key: "affects",
value: function affects(element, event) {
if (event.affects(element)) {
return true;
}
var _iterator2 = _createForOfIteratorHelper(this.findContribution(element)),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var contribution = _step2.value;
if (contribution.affects && contribution.affects(element, event)) {
return true;
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return false;
}
}, {
key: "onDidChange",
get: function get() {
return this.onDidChangeEmitter.event;
}
/**
* Return a default file icon for the current icon theme.
*/
}, {
key: "fileIcon",
get: function get() {
return this.getIcon(URIIconReference.create('file'));
}
/**
* Return a default folder icon for the current icon theme.
*/
}, {
key: "folderIcon",
get: function get() {
return this.getIcon(URIIconReference.create('folder'));
}
/**
* Get the icon class from the list of available {@link LabelProviderContribution} for the given element.
* @return the icon class
*/
}, {
key: "getIcon",
value: function getIcon(element) {
var contributions = this.findContribution(element);
var _iterator3 = _createForOfIteratorHelper(contributions),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var contribution = _step3.value;
var value = contribution.getIcon && contribution.getIcon(element);
if (value === undefined) {
continue;
}
return value;
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
return '';
}
/**
* Get a short name from the list of available {@link LabelProviderContribution} for the given element.
* @return the short name
*/
}, {
key: "getName",
value: function getName(element) {
var contributions = this.findContribution(element);
var _iterator4 = _createForOfIteratorHelper(contributions),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var contribution = _step4.value;
var value = contribution.getName && contribution.getName(element);
if (value === undefined) {
continue;
}
return value;
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
return '<unknown>';
}
/**
* Get a long name from the list of available {@link LabelProviderContribution} for the given element.
* @return the long name
*/
}, {
key: "getLongName",
value: function getLongName(element) {
var contributions = this.findContribution(element);
var _iterator5 = _createForOfIteratorHelper(contributions),
_step5;
try {
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
var contribution = _step5.value;
var value = contribution.getLongName && contribution.getLongName(element);
if (value === undefined) {
continue;
}
return value;
}
} catch (err) {
_iterator5.e(err);
} finally {
_iterator5.f();
}
return '';
}
/**
* Get a icon component from the list of available {@link LabelProviderContribution} for the given element.
* @return the icon component
*/
}, {
key: "getIconCompomponent",
value: function getIconCompomponent(element) {
var contributions = this.findContribution(element);
var _iterator6 = _createForOfIteratorHelper(contributions),
_step6;
try {
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
var contribution = _step6.value;
var value = contribution.getIconComponent && contribution.getIconComponent(element);
if (value === undefined) {
continue;
}
return value;
}
} catch (err) {
_iterator6.e(err);
} finally {
_iterator6.f();
}
return null;
}
/**
* Get a name component from the list of available {@link LabelProviderContribution} for the given element.
* @return the name component
*/
}, {
key: "getNameComponent",
value: function getNameComponent(element) {
var contributions = this.findContribution(element);
var _iterator7 = _createForOfIteratorHelper(contributions),
_step7;
try {
for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
var contribution = _step7.value;
var value = contribution.getNameComponent && contribution.getNameComponent(element);
if (value === undefined) {
continue;
}
return value;
}
} catch (err) {
_iterator7.e(err);
} finally {
_iterator7.f();
}
return '<unknown>';
}
/**
* Get a description component from the list of available {@link LabelProviderContribution} for the given element.
* @return the description component
*/
}, {
key: "getDescriptionComponent",
value: function getDescriptionComponent(element) {
var contributions = this.findContribution(element);
var _iterator8 = _createForOfIteratorHelper(contributions),
_step8;
try {
for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
var contribution = _step8.value;
var value = contribution.getDescriptionComponent && contribution.getDescriptionComponent(element);
if (value === undefined) {
continue;
}
return value;
}
} catch (err) {
_iterator8.e(err);
} finally {
_iterator8.f();
}
return null;
}
}, {
key: "findContribution",
value: function findContribution(element) {
var prioritized = _manaCommon.Priority.sortSync(this.contributionProvider.getContributions(), function (contribution) {
return contribution.canHandle(element);
});
return prioritized.map(function (c) {
return c.value;
});
}
}]);
return LabelProvider;
}()) || _class3);