From 9a3dd1ac87891bf223e67d6b8954854a9ae8492d Mon Sep 17 00:00:00 2001 From: Fevol Date: Thu, 9 Mar 2023 20:17:22 +0100 Subject: [PATCH] refactor: move functions to more logical places --- src/constants.ts | 45 +---------------- src/editor/commands.ts | 6 +-- src/editor/context-menu-commands.ts | 2 +- src/editor/criticmarkup-gutter.ts | 2 +- src/editor/editor-handlers.ts | 4 +- src/editor/{util.ts => editor-util.ts} | 69 ++++++-------------------- src/editor/file-menu-commands.ts | 29 ----------- src/editor/live-preview.ts | 2 +- src/editor/post-processor.ts | 2 +- src/main.ts | 2 +- src/util.ts | 49 ++++++++++++++++++ 11 files changed, 74 insertions(+), 138 deletions(-) rename src/editor/{util.ts => editor-util.ts} (67%) delete mode 100644 src/editor/file-menu-commands.ts create mode 100644 src/util.ts diff --git a/src/constants.ts b/src/constants.ts index 0ee73b3..ba033f0 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,48 +1,5 @@ -import type {PluginSettings} from "./types"; +import type { PluginSettings } from './types'; -export const CM_Syntax: {[key: string]: [string, string]} = { - "Addition": ["+", "+"], - "Deletion": ["-", "-"], - "Substitution": ["~", "~"], - "Highlight": ["=", "="], - "Comment": [">", "<"] -} - -export const CM_All_Brackets: {[key: string]: string[]} = { - "Addition": ["{++", "++}"], - "Deletion":["{--", "--}"], - "Substitution": ["{~~", "~>", "~~}"], - "Highlight": ["{==", "==}"], - "Comment": ["{>>", "<<}"] -} - -export const CM_Brackets: {[key: string]: string[]} = { - "{++": ["++}"], - "{--": ["--}"], - "{~~": ["~>", "~~}"], - "{==": ["==}"], - "{>>": ["<<}"] -} - - -export function replaceBracket(content: string, type: string) { - return wrapBracket(unwrapBracket(content), type); -} - -export function unwrapBracket(content: string) { - return content.slice(3, -3); -} - -export function wrapBracket(content: string, type: string) { - return CM_All_Brackets[type][0] + content + CM_All_Brackets[type].slice(1).join(''); -} - -export function addBracket(content: string, type: string, left: boolean) { - if (left) - return '{' + CM_Syntax[type][0].repeat(2) + content; - else - return content + CM_Syntax[type][1].repeat(2) + '}'; -} export const DEFAULT_SETTINGS: PluginSettings = { suggestion_status: 0, diff --git a/src/editor/commands.ts b/src/editor/commands.ts index 2b504de..191537f 100644 --- a/src/editor/commands.ts +++ b/src/editor/commands.ts @@ -1,13 +1,13 @@ import type { CommandI } from '../../types'; -import type { Editor, EditorChange, EditorTransaction, MarkdownView } from 'obsidian'; +import type { Editor, MarkdownView } from 'obsidian'; import type { Tree } from '@lezer/common'; import { criticmarkupLanguage } from './parser'; -import { addBracket, unwrapBracket, wrapBracket } from '../constants'; -import { ltEP, minEP, maxEP, nodesInSelection, selectionToRange } from './util'; +import { ltEP, minEP, maxEP, nodesInSelection, selectionToRange } from './editor-util'; import type { ChangeSpec } from '@codemirror/state'; import { EditorSelection } from '@codemirror/state'; +import { addBracket, unwrapBracket, wrapBracket } from '../util'; function changeSelectionType(editor: Editor, view: MarkdownView, type: string) { diff --git a/src/editor/context-menu-commands.ts b/src/editor/context-menu-commands.ts index 0e2f4cf..67b9984 100644 --- a/src/editor/context-menu-commands.ts +++ b/src/editor/context-menu-commands.ts @@ -1,6 +1,6 @@ import type { EventRef } from 'obsidian'; import { acceptAllSuggestions, rejectAllSuggestions } from './commands'; -import { selectionToRange } from './util'; +import { selectionToRange } from './editor-util'; export const change_suggestions: EventRef = diff --git a/src/editor/criticmarkup-gutter.ts b/src/editor/criticmarkup-gutter.ts index 66a274e..0a915e5 100644 --- a/src/editor/criticmarkup-gutter.ts +++ b/src/editor/criticmarkup-gutter.ts @@ -2,7 +2,7 @@ import { EditorView, gutter, GutterMarker, PluginValue, ViewPlugin } from '@code import { RangeSet, RangeSetBuilder } from '@codemirror/state'; import { Menu } from 'obsidian'; import { acceptAllSuggestions, rejectAllSuggestions } from './commands'; -import { nodesInSelection } from './util'; +import { nodesInSelection } from './editor-util'; export class CriticMarkupMarker extends GutterMarker { constructor(readonly from: number, readonly to: number, readonly type: string, readonly top?: boolean, readonly bottom?: boolean) { diff --git a/src/editor/editor-handlers.ts b/src/editor/editor-handlers.ts index 26e3aad..733d555 100644 --- a/src/editor/editor-handlers.ts +++ b/src/editor/editor-handlers.ts @@ -1,8 +1,8 @@ import { ChangeSpec, EditorSelection, EditorState, Prec } from '@codemirror/state'; import { EditorView } from '@codemirror/view'; -import { CM_Brackets } from '../constants'; import { criticmarkupLanguage } from './parser'; -import { moveEditorCursor, nodeAtCursor } from './util'; +import { moveEditorCursor, nodeAtCursor } from './editor-util'; +import { CM_Brackets } from '../util'; export const bracketMatcher = Prec.high(EditorView.inputHandler.of((view, from, to, text) => { const before = view.state.doc.sliceString(from - 2, from) + text; diff --git a/src/editor/util.ts b/src/editor/editor-util.ts similarity index 67% rename from src/editor/util.ts rename to src/editor/editor-util.ts index 224a9eb..59b02e6 100644 --- a/src/editor/util.ts +++ b/src/editor/editor-util.ts @@ -1,7 +1,6 @@ -import type { EditorPosition } from 'obsidian'; +import type { Editor, EditorPosition } from 'obsidian'; import type { Tree } from '@lezer/common'; import { EditorSelection } from '@codemirror/state'; -import type { Editor } from 'obsidian'; export function eqEP(a: EditorPosition, b: EditorPosition): boolean { @@ -28,6 +27,16 @@ export function maxEP(a: EditorPosition, b: EditorPosition): EditorPosition { return ltEP(a, b) ? b : a; } +export function moveEditorCursor(selection: EditorSelection, change_start: number, offset: number) { + if (change_start >= selection.ranges[0].from) + return selection; + return EditorSelection.range( + selection.ranges[0].from + offset, + selection.ranges[0].to + offset, + ) +} + + export function selectionToRange(editor: Editor): number[] { const selection = editor.listSelections()[0]; @@ -43,6 +52,8 @@ export function selectionRangeOverlap(selection: EditorSelection, rangeFrom: num } + + export function nodeAtCursor(tree: Tree, pos: number) { const node = tree.resolve(pos, -1); if (node.type.name === '⚠' || node.type.name === 'CriticMarkup') @@ -52,14 +63,6 @@ export function nodeAtCursor(tree: Tree, pos: number) { return node; } -export function moveEditorCursor(selection: EditorSelection, change_start: number, offset: number) { - if (change_start >= selection.ranges[0].from) - return selection; - return EditorSelection.range( - selection.ranges[0].from + offset, - selection.ranges[0].to + offset, - ) -} export function nodesInSelection(tree: Tree, start?: number, end?: number) { @@ -92,48 +95,4 @@ export function nodesInSelection(tree: Tree, start?: number, end?: number) { }, }); return nodes; -} - -export function objectDifference(new_obj: any, old_obj: any): Partial { - const diff: Partial = {}; - for (const key in new_obj) { - if (new_obj[key] !== old_obj[key]) - diff[key] = new_obj[key]; - } - return diff; -} - - -// function nodesInText(tree: Tree) { -// const nodes: { from: number, middle?: number, to: number, type: string }[] = []; -// -// const cursor = tree.cursor(); -// while (cursor.next()) { -// const start = cursor.from; -// const end = cursor.to; -// const name = cursor.name; -// -// // If error detected: return only the confirmed nodes (errored node will always contain all text after it, invalid) -// if (name === '⚠') -// return nodes.slice(0, -1); -// -// if (name === 'Substitution') { -// cursor.firstChild(); -// if (cursor.name !== 'MSub') continue; -// -// nodes.push({ -// from: start, -// middle: cursor.from, -// to: end, -// type: name, -// }); -// } else { -// nodes.push({ -// from: start, -// to: end, -// type: name, -// }); -// } -// } -// return nodes; -// } \ No newline at end of file +} \ No newline at end of file diff --git a/src/editor/file-menu-commands.ts b/src/editor/file-menu-commands.ts deleted file mode 100644 index 2accfbc..0000000 --- a/src/editor/file-menu-commands.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { EventRef } from 'obsidian'; -import type { ItemView } from 'obsidian'; -import { setIcon } from 'obsidian'; - -// Partially adapted from Commander plugin -let button_mapping = new WeakMap(); - -const status_mapping = [ - { icon: "message-square", label: "Preview \"accept all\"" }, - { icon: "check", label: "Preview \"accept all\"" }, - { icon: "cross", label: "Preview \"reject all\"" }, -]; - -export const file_view_modes: EventRef = - app.workspace.on("layout-change", () => { - for (const leaf of app.workspace.getLeavesOfType("markdown")) { - const view = leaf.view as ItemView; - if (button_mapping.has(view)) continue; - - let status = 1; - const buttonElement = view.addAction("message-square", "View all suggestions", () => { - const { icon, label } = status_mapping[status]; - setIcon(buttonElement, icon); - buttonElement.setAttribute("aria-label", label); - status = (status + 1) % status_mapping.length; - }); - button_mapping.set(view, buttonElement); - } - }) \ No newline at end of file diff --git a/src/editor/live-preview.ts b/src/editor/live-preview.ts index 7b06d18..6a5fb37 100644 --- a/src/editor/live-preview.ts +++ b/src/editor/live-preview.ts @@ -15,7 +15,7 @@ import { TreeFragment } from '@lezer/common'; import { RangeSet } from '@codemirror/state'; import { buildMarkers, CriticMarkupMarker, gutterExtension } from './criticmarkup-gutter'; import type { PluginSettings } from '../types'; -import { selectionRangeOverlap } from './util'; +import { selectionRangeOverlap } from './editor-util'; export function inlinePlugin(settings: PluginSettings): Extension[] { const view_plugin = ViewPlugin.fromClass( diff --git a/src/editor/post-processor.ts b/src/editor/post-processor.ts index 113c4dc..7cf1924 100644 --- a/src/editor/post-processor.ts +++ b/src/editor/post-processor.ts @@ -1,7 +1,7 @@ import { criticmarkupLanguage } from './parser'; -import { CM_Syntax } from '../constants'; import type {MarkdownView} from "obsidian"; +import { CM_Syntax } from '../util'; export function postProcess(el: HTMLElement, ctx: any, settings: any) { const tree = criticmarkupLanguage.parser.parse(el.innerHTML); diff --git a/src/main.ts b/src/main.ts index 0fee4b9..182860f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,8 +16,8 @@ import { bracketMatcher, nodeCorrecter } from './editor/editor-handlers'; import type {PluginSettings} from "./types"; import {DEFAULT_SETTINGS} from "./constants"; import {loadEditorButtons, removeEditorButtons} from "./editor/editor-preview-buttons"; -import {objectDifference} from "./editor/util"; import {CommentatorSettings} from "./ui/settings"; +import { objectDifference } from './util'; export default class CommentatorPlugin extends Plugin { private editorExtensions: Extension[] = []; diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..9fafe6d --- /dev/null +++ b/src/util.ts @@ -0,0 +1,49 @@ +export function objectDifference(new_obj: any, old_obj: any): Partial { + const diff: Partial = {}; + for (const key in new_obj) { + if (new_obj[key] !== old_obj[key]) + diff[key] = new_obj[key]; + } + return diff; +} + +export const CM_Syntax: { [key: string]: [string, string] } = { + 'Addition': ['+', '+'], + 'Deletion': ['-', '-'], + 'Substitution': ['~', '~'], + 'Highlight': ['=', '='], + 'Comment': ['>', '<'], +}; +export const CM_All_Brackets: { [key: string]: string[] } = { + 'Addition': ['{++', '++}'], + 'Deletion': ['{--', '--}'], + 'Substitution': ['{~~', '~>', '~~}'], + 'Highlight': ['{==', '==}'], + 'Comment': ['{>>', '<<}'], +}; +export const CM_Brackets: { [key: string]: string[] } = { + '{++': ['++}'], + '{--': ['--}'], + '{~~': ['~>', '~~}'], + '{==': ['==}'], + '{>>': ['<<}'], +}; + +export function replaceBracket(content: string, type: string) { + return wrapBracket(unwrapBracket(content), type); +} + +export function unwrapBracket(content: string) { + return content.slice(3, -3); +} + +export function wrapBracket(content: string, type: string) { + return CM_All_Brackets[type][0] + content + CM_All_Brackets[type].slice(1).join(''); +} + +export function addBracket(content: string, type: string, left: boolean) { + if (left) + return '{' + CM_Syntax[type][0].repeat(2) + content; + else + return content + CM_Syntax[type][1].repeat(2) + '}'; +} \ No newline at end of file