Files
OSIT-AE-App-Svelte/src/lib/ae_journals/ae_journals_editor_helpers.ts
Scott Idem 2f3125c64b style(journals): apply expanded 80-width formatting and snake_case
- Batch formatted all Journals module files using Prettier with printWidth: 80.
- Refactored preventDefault to prevent_default across all Svelte components.
- Standardized line breaks for imports and long attribute lists for better readability.
- Ensured consistent snake_case naming for internal identifiers.
2026-02-06 14:15:43 -05:00

80 lines
2.3 KiB
TypeScript

/**
* Wraps the current selection in CodeMirror with the given prefix and suffix.
*/
export function wrapSelection(
view: any,
prefix: string,
suffix: string = prefix
) {
if (!view || view.updateInProgress) return;
const { state } = view;
view.dispatch(
state.changeByRange((range: any) => {
return {
changes: [
{ from: range.from, insert: prefix },
{ from: range.to, insert: suffix }
],
range: range.constructor.range(
range.from + prefix.length,
range.to + prefix.length
)
};
})
);
view.focus();
}
/**
* Inserts a prefix at the start of each line in the selection (e.g., for lists or blockquotes).
*/
export function toggleLinePrefix(view: any, prefix: string) {
if (!view || view.updateInProgress) return;
const { state } = view;
view.dispatch(
state.changeByRange((range: any) => {
const lines = [];
for (let pos = range.from; pos <= range.to; ) {
const line = state.doc.lineAt(pos);
lines.push(line);
pos = line.to + 1;
}
const isAlreadyPrefixed = lines.every((l) =>
l.text.startsWith(prefix)
);
const lineChanges = lines.map((l) => {
if (isAlreadyPrefixed) {
return {
from: l.from,
to: l.from + prefix.length,
insert: ''
};
} else {
return { from: l.from, insert: prefix };
}
});
const newFrom =
range.from +
(isAlreadyPrefixed ? -prefix.length : prefix.length);
const newTo =
range.to +
(isAlreadyPrefixed
? -prefix.length * lines.length
: prefix.length * lines.length);
return {
changes: lineChanges,
range: range.constructor.range(
newFrom,
Math.max(newFrom, newTo)
)
};
})
);
view.focus();
}