@gooddata/react-components
Version:
GoodData.UI - A powerful JavaScript library for building analytical applications
60 lines • 2.72 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
// (C) 2020 GoodData Corporation
var React = require("react");
var classNames = require("classnames");
var react_codemirror2_1 = require("react-codemirror2");
var CodeMirror = require("codemirror");
require("codemirror/lib/codemirror.css");
require("codemirror/addon/mode/simple");
require("codemirror/addon/edit/matchbrackets");
require("codemirror/addon/display/placeholder");
var CODE_MIRROR_EOL = "\n";
var defaultOptions = {
autofocus: false,
lineWrapping: true,
matchBrackets: true,
};
var findCursorIndexAcrossMultipleLines = function (text, cursorLineIndex, cursorLineCharacterIndex) {
var getLineLengthToCursor = function (line, lineIndex) {
if (lineIndex > cursorLineIndex) {
return 0;
}
return lineIndex < cursorLineIndex ? line.length + CODE_MIRROR_EOL.length : cursorLineCharacterIndex;
};
return text
.split(CODE_MIRROR_EOL)
.map(getLineLengthToCursor)
.reduce(function (sum, lineLength) { return sum + lineLength; }, 0);
};
exports.SyntaxHighlightingInput = function (props) {
var value = props.value, onChange = props.onChange, onCursor = props.onCursor, formatting = props.formatting, customOptions = props.customOptions, className = props.className;
var reportCursorPosition = function (editor) {
if (onCursor) {
var from = editor.getCursor("from");
var to = editor.getCursor("to");
var currentValue = editor.getValue();
onCursor(findCursorIndexAcrossMultipleLines(currentValue, from.line, from.ch), findCursorIndexAcrossMultipleLines(currentValue, to.line, to.ch));
}
};
var handleOnChange = function (_editor, _change, value) {
onChange(value);
};
var modeOptions = formatting && {
name: "format",
fn: CodeMirror.defineSimpleMode("syntaxHighlight", formatting),
};
return (React.createElement(react_codemirror2_1.UnControlled, { className: classNames(className, "gd-input-syntax-highlighting-input"), value: value, defineMode: modeOptions, onChange: handleOnChange, onCursorActivity: reportCursorPosition, autoCursor: false, options: __assign({}, defaultOptions, customOptions) }));
};
//# sourceMappingURL=SyntaxHighlightingInput.js.map