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

✨ Improved Formatting functionality #85

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 56 additions & 22 deletions src/api/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,25 +266,59 @@ const getChanges = (changeDetails: ChangeDetails) => {
return formatInline(changeDetails)
}

export const format = ([state]: InkInternal.Store, formatType: Ink.EnumString<Ink.Values.Markup>, { selection: maybeSelection }: Ink.Instance.FormatOptions = {}) => {
const { editor } = state()
const formatDefinition = formatting[formatType]
const selection = getSelection({ editor, formatDefinition, selection: maybeSelection })
const node = getNode(editor, formatDefinition, selection)
const changeDetails: ChangeDetails = {
editor,
formatDefinition,
node,
selection,
}
const changes = getChanges(changeDetails)
const offset = changes.reduce((total, change: { from: number, insert: string, to?: number }) => {
const offset = change.insert.length - ((change.to || change.from) - change.from)

return total + offset
}, 0)

const updates = state().editor.state.update({ changes, selection: { head: selection.start, anchor: selection.end + offset } })

state().editor.dispatch(updates)
}
export const format = (
[state]: InkInternal.Store,
formatType: Ink.EnumString<Ink.Values.Markup>,
{ selection: maybeSelection }: Ink.Instance.FormatOptions = {}
) => {
const { editor } = state();
const formatDefinition = formatting[formatType];
const selection = getSelection({
editor,
formatDefinition,
selection: maybeSelection,
});

// Check if the selection is empty
if (selection.start === selection.end) {
// Insert the prefix and suffix around the cursor
const changes = [
{ from: selection.start, insert: formatDefinition.prefix },
{ from: selection.start, insert: formatDefinition.suffix },
];
const cursorPosition = selection.start + formatDefinition.prefix.length;
const updates = state().editor.state.update({
changes,
selection: { head: cursorPosition, anchor: cursorPosition },
});

state().editor.dispatch(updates);
state().editor.focus();
} else {
const node = getNode(editor, formatDefinition, selection);
const changeDetails: ChangeDetails = {
editor,
formatDefinition,
node,
selection,
};
const changes = getChanges(changeDetails);
const offset = changes.reduce(
(total, change: { from: number; insert: string; to?: number }) => {
const offset =
change.insert.length - ((change.to || change.from) - change.from);

return total + offset;
},
0
);

const updates = state().editor.state.update({
changes,
selection: { head: selection.start, anchor: selection.end + offset },
});

state().editor.dispatch(updates);
state().editor.focus();
}
};