Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cursorOffset to Playground #15751

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 64 additions & 2 deletions website/playground/Playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ class Playground extends React.Component {
this.setContent = (content) => this.setState({ content });
this.clearContent = this.setContent.bind(this, "");
this.resetOptions = () => this.setState({ options: defaultOptions });
this.setSelection = (selection) => this.setState({ selection });
this.setSelection = (selection) => {
this.setState({ selection });
if (this.state.trackCursorOffset) {
this.handleOptionValueChange(
this.cursorOffsetOption,
util.convertSelectionToRange(selection, this.state.content)[0],
);
}
};
this.setSelectionAsRange = () => {
const { selection, content, options } = this.state;
const [rangeStart, rangeEnd] = util.convertSelectionToRange(
Expand All @@ -126,6 +134,9 @@ class Playground extends React.Component {
this.rangeEndOption = props.availableOptions.find(
(opt) => opt.name === "rangeEnd",
);
this.cursorOffsetOption = props.availableOptions.find(
(opt) => opt.name === "cursorOffset",
);

this.formatInput = this.formatInput.bind(this);
this.insertDummyId = this.insertDummyId.bind(this);
Expand Down Expand Up @@ -172,6 +183,7 @@ class Playground extends React.Component {
...ENABLED_OPTIONS,
"rangeStart",
"rangeEnd",
"cursorOffset",
]);
const cliOptions = util.buildCliArgs(orderedOptions, options);

Expand Down Expand Up @@ -247,7 +259,7 @@ class Playground extends React.Component {
reformat={editorState.showSecondFormat}
rethrowEmbedErrors={editorState.rethrowEmbedErrors}
>
{({ formatted, debug }) => {
{({ formatted, debug, cursorOffset }) => {
const fullReport = this.getMarkdown({
formatted,
reformatted: debug.reformatted,
Expand Down Expand Up @@ -294,6 +306,50 @@ class Playground extends React.Component {
Set selected text as range
</Button>
</SidebarCategory>
<SidebarCategory title="Cursor">
<Option
option={this.cursorOffsetOption}
value={
typeof options.cursorOffset === "number" &&
options.cursorOffset !== -1
? options.cursorOffset
: ""
}
onChange={this.handleOptionValueChange}
/>
<div
style={{
display: "flex",
alignItems: "baseline",
gap: "10px",
}}
>
<Checkbox
label="track"
checked={Boolean(this.state.trackCursorOffset)}
onChange={() =>
this.setState((state) => ({
trackCursorOffset: !state.trackCursorOffset,
}))
}
/>
{options.cursorOffset !== -1 && (
<>
<Button
onClick={() => {
this.handleOptionValueChange(
this.cursorOffsetOption,
-1,
);
}}
>
Reset
</Button>
<label>Result: {cursorOffset}</label>
</>
)}
</div>
</SidebarCategory>
<SidebarCategory title="Debug">
<Checkbox
label="show input"
Expand Down Expand Up @@ -419,6 +475,12 @@ class Playground extends React.Component {
mode={util.getCodemirrorMode(options.parser)}
value={formatted}
ruler={options.printWidth}
overlayStart={
cursorOffset === -1 ? undefined : cursorOffset
}
overlayEnd={
cursorOffset === -1 ? undefined : cursorOffset + 1
}
/>
)
) : null}
Expand Down
8 changes: 4 additions & 4 deletions website/playground/panels.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ class CodeMirrorPanel extends React.Component {
}

updateOverlay() {
if (!this.props.readOnly) {
if (this._overlay) {
this._codeMirror.removeOverlay(this._overlay);
}
if (this._overlay) {
this._codeMirror.removeOverlay(this._overlay);
}
if (this.props.overlayStart !== undefined) {
const [start, end] = getIndexPosition(this.props.value, [
this.props.overlayStart,
this.props.overlayEnd,
Expand Down