Files
react-native-devtools-frontend/front_end/changes/ChangesTextEditor.js
T
Christy Chen 5d7848d54d Reland "[Loc] Migrate changes tool to localization V2"
This reverts commit 9e5ab71e50.

Reason for revert: The Cl was reverted due to devtools pak file limitation. That issue has been fixed in https://chromium-review.googlesource.com/c/chromium/src/+/2558341

Original change's description:
> Revert "[Loc] Migrate changes tool to localization V2"
>
> This reverts commit abcb8f7aa8.
>
> Reason for revert: Consistently causing autoroll issues when rolling devtools-frontend into Chromium - see https://chromium-review.googlesource.com/c/chromium/src/+/2554521 for an example.
>
> To recreate this, do a debug build of Chromium with this patch applied to DevTools. It's causing an issue with duplicate keys in the pak files. No DevTools Frontend builds do a debug build of Chromium with the DevTools frontend patch applied so our CQ will be green despite causing an issue.
>
> You can run this locally in Chromium to reproduce via:
>
> autoninja -C out/Default chrome:packed_resources_extra
>
> Which will show the error. 
>
> Original change's description:
> > [Loc] Migrate changes tool to localization V2
> >
> > Migrate changes tool
> > Tested with en-XL: https://imgur.com/a/hFDBjQV
> >
> >
> > Bug: 1136655
> > Change-Id: Ic434c3085b9e2f2cf44583cca7ee1ce8f1d412f2
> > Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2548688
> > Commit-Queue: Christy Chen <chrche@microsoft.com>
> > Reviewed-by: Simon Zünd <szuend@chromium.org>
>
> TBR=szuend@chromium.org,chrche@microsoft.com
>
> Change-Id: I94b27434562f6a6bff3be381ab8a5802bd8b03f9
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: 1136655
> Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2557044
> Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
> Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>

TBR=szuend@chromium.org,chrche@microsoft.com,jacktfranklin@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 1136655
Change-Id: I35166a6f4f38462073b933c01c169b3c8d14311b
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2567344
Reviewed-by: Simon Zünd <szuend@chromium.org>
Reviewed-by: Christy Chen <chrche@microsoft.com>
Commit-Queue: Christy Chen <chrche@microsoft.com>
2020-12-02 06:13:17 +00:00

120 lines
4.3 KiB
JavaScript

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as i18n from '../i18n/i18n.js';
import * as TextEditor from '../text_editor/text_editor.js';
import * as UI from '../ui/ui.js'; // eslint-disable-line no-unused-vars
import {Row, RowType} from './ChangesView.js'; // eslint-disable-line no-unused-vars
export const UIStrings = {
/**
*@description Text prepended to a removed line in a diff in the Changes tool, viewable only by screen reader.
*@example {function log () } PH1
*/
deletions: 'Deletion:{PH1}',
/**
*@description Text prepended to a new line in a diff in the Changes tool, viewable only by screen reader.
*@example {function log () } PH1
*/
additions: 'Addition:{PH1}',
};
const str_ = i18n.i18n.registerUIStrings('changes/ChangesTextEditor.js', UIStrings);
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
/**
* @extends {TextEditor.CodeMirrorTextEditor.CodeMirrorTextEditor}
*/
export class ChangesTextEditor extends TextEditor.CodeMirrorTextEditor.CodeMirrorTextEditor {
/**
* @param {!UI.TextEditor.Options} options
*/
constructor(options) {
options.inputStyle = 'devToolsAccessibleDiffTextArea';
super(options);
this.codeMirror().setOption('gutters', ['CodeMirror-linenumbers', 'changes-diff-gutter']);
this.codeMirror().setOption('extraKeys', {
Enter: false,
Space: false,
/**
* @param {!CodeMirror.Editor} cm
*/
Left: function(cm) {
const scrollInfo = cm.getScrollInfo();
// Left edge check required due to bug where line numbers would disappear when attempting to scroll left when the scrollbar is at the leftmost point.
// CodeMirror Issue: https://github.com/codemirror/CodeMirror/issues/6139
if (scrollInfo.left > 0) {
cm.scrollTo(scrollInfo.left - Math.round(scrollInfo.clientWidth / 6), null);
}
},
/**
* @param {!CodeMirror.Editor} cm
*/
Right: function(cm) {
const scrollInfo = cm.getScrollInfo();
cm.scrollTo(scrollInfo.left + Math.round(scrollInfo.clientWidth / 6), null);
}
});
}
/**
* @param {!Array<!Row>} diffRows
*/
updateDiffGutter(diffRows) {
this.codeMirror().eachLine(/** @param {!CodeMirror.LineHandle} line */ line => {
const lineNumber = this.codeMirror().getLineNumber(line);
const row = diffRows[lineNumber];
let gutterMarker;
if (row.type === RowType.Deletion) {
gutterMarker = document.createElement('div');
gutterMarker.classList.add('deletion');
gutterMarker.classList.add('changes-diff-gutter-marker');
gutterMarker.textContent = '-';
} else if (row.type === RowType.Addition) {
gutterMarker = document.createElement('div');
gutterMarker.classList.add('addition');
gutterMarker.classList.add('changes-diff-gutter-marker');
gutterMarker.textContent = '+';
}
if (gutterMarker) {
this.codeMirror().setGutterMarker(line, 'changes-diff-gutter', gutterMarker);
}
});
}
}
export class DevToolsAccessibleDiffTextArea extends TextEditor.CodeMirrorTextEditor.DevToolsAccessibleTextArea {
/**
* @override
* @param {boolean=} typing - whether the user is currently typing
*/
reset(typing) {
super.reset(typing);
// TODO(crbug.com/1011811): Update CodeMirror typings to include this property
const doc = /** @type {!CodeMirror.Doc} */ (/** @type {*} */ (this.cm).doc);
if (this.textAreaBusy(!!typing) || !(typeof doc.modeOption === 'object')) {
return;
}
const diffRows = doc.modeOption.diffRows;
const lineNumber = this.cm.getCursor().line;
const rowType = diffRows[lineNumber].type;
if (rowType === RowType.Deletion) {
this.textarea.value = i18nString(UIStrings.deletions, {PH1: this.textarea.value});
}
if (rowType === RowType.Addition) {
this.textarea.value = i18nString(UIStrings.additions, {PH1: this.textarea.value});
}
this.prevInput = this.textarea.value;
}
}
/**
* @constructor
*/
// @ts-ignore CodeMirror integration with externals, not yet part of codemirror-legacy.d.ts
CodeMirror.inputStyles.devToolsAccessibleDiffTextArea = DevToolsAccessibleDiffTextArea;