import BaseFormatter from './base.js'; export default class AnnotatedFormatter extends BaseFormatter { constructor() { super(); this.includeMoveDestinations = false; } prepareContext(context) { super.prepareContext(context); context.indent = function(levels) { this.indentLevel = (this.indentLevel || 0) + (typeof levels === 'undefined' ? 1 : levels); this.indentPad = new Array(this.indentLevel + 1).join('  '); }; context.row = (json, htmlNote) => { context.out( '' + '
'
      );
      context.out(context.indentPad);
      context.out('
');
      context.out(json);
      context.out('
'); context.out(htmlNote); context.out('
'); }; } typeFormattterErrorFormatter(context, err) { context.row('', `
${err}
`); } formatTextDiffString(context, value) { const lines = this.parseTextDiff(value); context.out(''); } rootBegin(context, type, nodeType) { context.out(''); if (type === 'node') { context.row('{'); context.indent(); } if (nodeType === 'array') { context.row( '"_t": "a",', 'Array delta (member names indicate array indices)' ); } } rootEnd(context, type) { if (type === 'node') { context.indent(-1); context.row('}'); } context.out('
'); } nodeBegin(context, key, leftKey, type, nodeType) { context.row(`"${key}": {`); if (type === 'node') { context.indent(); } if (nodeType === 'array') { context.row( '"_t": "a",', 'Array delta (member names indicate array indices)' ); } } nodeEnd(context, key, leftKey, type, nodeType, isLast) { if (type === 'node') { context.indent(-1); } context.row(`}${isLast ? '' : ','}`); } /* jshint camelcase: false */ /* eslint-disable camelcase */ format_unchanged() {} format_movedestination() {} format_node(context, delta, left) { // recurse this.formatDeltaChildren(context, delta, left); } } /* eslint-enable camelcase */ const wrapPropertyName = name => `
"${name}"
`; const deltaAnnotations = { added(delta, left, key, leftKey) { const formatLegend = '
([newValue])
'; if (typeof leftKey === 'undefined') { return `new value${formatLegend}`; } if (typeof leftKey === 'number') { return `insert at index ${leftKey}${formatLegend}`; } return `add property ${wrapPropertyName(leftKey)}${formatLegend}`; }, modified(delta, left, key, leftKey) { const formatLegend = '
([previousValue, newValue])
'; if (typeof leftKey === 'undefined') { return `modify value${formatLegend}`; } if (typeof leftKey === 'number') { return `modify at index ${leftKey}${formatLegend}`; } return `modify property ${wrapPropertyName(leftKey)}${formatLegend}`; }, deleted(delta, left, key, leftKey) { const formatLegend = '
([previousValue, 0, 0])
'; if (typeof leftKey === 'undefined') { return `delete value${formatLegend}`; } if (typeof leftKey === 'number') { return `remove index ${leftKey}${formatLegend}`; } return `delete property ${wrapPropertyName(leftKey)}${formatLegend}`; }, moved(delta, left, key, leftKey) { return ( 'move from ' + `index ${leftKey} to index ${delta[1]}` ); }, textdiff(delta, left, key, leftKey) { const location = typeof leftKey === 'undefined' ? '' : typeof leftKey === 'number' ? ` at index ${leftKey}` : ` at property ${wrapPropertyName(leftKey)}`; return ( `text diff${location}, format is a variation of Unidiff' ); }, }; const formatAnyChange = function(context, delta) { const deltaType = this.getDeltaType(delta); const annotator = deltaAnnotations[deltaType]; const htmlNote = annotator && annotator.apply(annotator, Array.prototype.slice.call(arguments, 1)); let json = JSON.stringify(delta, null, 2); if (deltaType === 'textdiff') { // split text diffs lines json = json.split('\\n').join('\\n"+\n "'); } context.indent(); context.row(json, htmlNote); context.indent(-1); }; /* eslint-disable camelcase */ AnnotatedFormatter.prototype.format_added = formatAnyChange; AnnotatedFormatter.prototype.format_modified = formatAnyChange; AnnotatedFormatter.prototype.format_deleted = formatAnyChange; AnnotatedFormatter.prototype.format_moved = formatAnyChange; AnnotatedFormatter.prototype.format_textdiff = formatAnyChange; /* eslint-enable camelcase */