@awesome-fe/translate
Version:
Translation utils
86 lines • 3.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseNodeRenderer = void 0;
const match_some_1 = require("./utils/match-some");
const lodash_1 = require("lodash");
class BaseNodeRenderer {
ignoredAttributeNames = [];
globalIgnoredAttributeNames = ['attribute_entries', 'title', 'target'];
positionalAttributes = [];
inlineAttributeNames = [];
isInlineAttribute(it) {
return (0, match_some_1.matchSome)(it.name, this.inlineAttributeNames);
}
getAttributes(node) {
const $$keys = node.attributes.$$keys;
const result = $$keys
.map(($$key) => {
if (typeof $$key === 'string') {
const position = this.positionalAttributes.find(it => it.name === $$key)?.position;
return [
{ position: undefined, name: $$key, value: node.getAttribute($$key), negate: false },
position && { position: position, name: $$key, value: node.getAttribute($$key), negate: false },
];
}
const positionalAttribute = this.positionalAttributes.find(it => it.position === $$key.key);
if (positionalAttribute) {
const name = positionalAttribute.name;
// 由于 setAttribute 不会更改 $$keys 中的 value,因此这里要重新取一下属性值
const value = node.getAttribute(name);
return { position: $$key.key, name, value, negate: false };
}
else {
return { position: $$key.key, name: '1', value: $$key.value, negate: false };
}
})
.flat()
.filter(it => !!it)
.filter(it => !(0, match_some_1.matchSome)(it.name, [...this.globalIgnoredAttributeNames, ...this.ignoredAttributeNames]));
const uniqAttributes = (0, lodash_1.uniqBy)(result, it => `${it.position}-${it.name}=${it.value}`);
return moveIdToFirst(uniqAttributes.filter(it => !correspondingPositionalExists(it, result)));
}
getDefaultAttributes(node) {
return {};
}
getNonDefaultAttributes(node) {
const defaultAttributes = this.getDefaultAttributes(node);
return this.getAttributes(node).map((attr) => {
if (defaultAttributes[attr.name] === attr.value) {
return;
}
else if (attr.name === 'options') {
return {
...attr,
value: attr.value.split(',').map(subValue => subValue.trim())
.filter(it => defaultAttributes[attr.name] !== it)
.join(','),
};
}
else {
return attr;
}
}).filter(it => !!it);
}
getBlockAttributes(node) {
return this.getNonDefaultAttributes(node).filter(it => !this.isInlineAttribute(it));
}
getInlineAttributes(node) {
return this.getNonDefaultAttributes(node).filter(it => this.isInlineAttribute(it));
}
}
exports.BaseNodeRenderer = BaseNodeRenderer;
function correspondingPositionalExists(attribute, existingAttributes) {
return !attribute.position && !!existingAttributes.find(it => {
return it.position && it.name === attribute.name && it.value === attribute.value;
});
}
function moveIdToFirst(attributes) {
const id = attributes.find(it => it.name === 'id');
if (!id) {
return attributes;
}
else {
return [id, ...attributes.filter(it => it !== id)];
}
}
//# sourceMappingURL=base-node-renderer.js.map