@blinkk/editor
Version:
Structured content editor with live previews.
54 lines • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LiveEditorAutoFields = void 0;
const media_1 = require("./field/media");
const autoFields_1 = require("@blinkk/selective-edit/dist/src/selective/autoFields");
const dataType_1 = require("@blinkk/selective-edit/dist/src/utility/dataType");
class LiveEditorAutoFields extends autoFields_1.AutoFields {
deepGuessObject(data, keyBase) {
keyBase = keyBase || [];
// Check for media field.
if (data.url && dataType_1.DataType.isString(data.url)) {
if (this.isMediaValue(data.url)) {
const key = keyBase.join('.');
return [
{
key: key,
type: 'media',
label: this.guessLabel(key),
},
];
}
}
return super.deepGuessObject(data, keyBase);
}
/**
* Guess the type of field to use based on the key and value.
*
* @param key Key to guess the type of field.
* @param data Data to use for guessing field type.
*/
guessType(key, data) {
// Check for media list field.
if (dataType_1.DataType.isArray(data)) {
if (data.length > 0 && data[0].url && dataType_1.DataType.isString(data[0].url)) {
if (this.isMediaValue(data[0].url)) {
// TODO: Detect extra fields as fields on the media list.
return 'mediaList';
}
}
}
return super.guessType(key, data);
}
isMediaValue(value) {
for (const ext of Object.keys(media_1.EXT_TO_MIME_TYPE)) {
if (value.endsWith(`.${ext}`)) {
return true;
}
}
// Support google images.
return /\.googleusercontent\.com/.test(value);
}
}
exports.LiveEditorAutoFields = LiveEditorAutoFields;
//# sourceMappingURL=autoFields.js.map