Skip to content

Commit

Permalink
chore: file clean-up, reformatting, prettifying imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Fevol committed Mar 9, 2023
1 parent 9a3dd1a commit 4672442
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 139 deletions.
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const DEFAULT_SETTINGS: PluginSettings = {
node_correcter: true,

post_processor: true,
live_preview: true
}
live_preview: true,
};
16 changes: 9 additions & 7 deletions src/editor/commands.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { CommandI } from '../../types';
import type { Editor, MarkdownView } from 'obsidian';

import type { ChangeSpec } from '@codemirror/state';
import { EditorSelection } from '@codemirror/state';
import type { Tree } from '@lezer/common';

import { criticmarkupLanguage } from './parser';
import { ltEP, minEP, maxEP, nodesInSelection, selectionToRange } from './editor-util';
import type { ChangeSpec } from '@codemirror/state';
import { EditorSelection } from '@codemirror/state';

import type { CommandI } from '../../types';

import { addBracket, unwrapBracket, wrapBracket } from '../util';
import { ltEP, minEP, maxEP, nodesInSelection, selectionToRange } from './editor-util';


function changeSelectionType(editor: Editor, view: MarkdownView, type: string) {
Expand Down Expand Up @@ -288,7 +290,7 @@ export const commands: Array<CommandI> = [...suggestion_commands,
editor.cm.dispatch(editor.cm.state.update({
changes: acceptAllSuggestions(editor.getValue(), from, to),
}));
}
},
},
{
id: 'commentator-reject-selected-suggestions',
Expand All @@ -301,6 +303,6 @@ export const commands: Array<CommandI> = [...suggestion_commands,
editor.cm.dispatch(editor.cm.state.update({
changes: rejectAllSuggestions(editor.getValue(), from, to),
}));
}
}
},
},
];
3 changes: 2 additions & 1 deletion src/editor/context-menu-commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EventRef } from 'obsidian';
import { acceptAllSuggestions, rejectAllSuggestions } from './commands';

import { selectionToRange } from './editor-util';
import { acceptAllSuggestions, rejectAllSuggestions } from './commands';


export const change_suggestions: EventRef =
Expand Down
7 changes: 5 additions & 2 deletions src/editor/criticmarkup-gutter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { EditorView, gutter, GutterMarker, PluginValue, ViewPlugin } from '@codemirror/view';
import { RangeSet, RangeSetBuilder } from '@codemirror/state';
import { Menu } from 'obsidian';

import { RangeSet, RangeSetBuilder } from '@codemirror/state';
import { EditorView, gutter, GutterMarker, PluginValue, ViewPlugin } from '@codemirror/view';

import { acceptAllSuggestions, rejectAllSuggestions } from './commands';

import { nodesInSelection } from './editor-util';

export class CriticMarkupMarker extends GutterMarker {
Expand Down
12 changes: 7 additions & 5 deletions src/editor/editor-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ChangeSpec, EditorSelection, EditorState, Prec } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
import { ChangeSpec, EditorSelection, EditorState, Prec } from '@codemirror/state';

import { criticmarkupLanguage } from './parser';
import { moveEditorCursor, nodeAtCursor } from './editor-util';

import { CM_Brackets } from '../util';
import { moveEditorCursor, nodeAtCursor } from './editor-util';

export const bracketMatcher = Prec.high(EditorView.inputHandler.of((view, from, to, text) => {
const before = view.state.doc.sliceString(from - 2, from) + text;
Expand All @@ -22,13 +24,13 @@ export const bracketMatcher = Prec.high(EditorView.inputHandler.of((view, from,
return true;
}
return false;
}))
}));

export const nodeCorrecter = EditorState.transactionFilter.of(tr => {
// From 'tr', get the spec from a decoration transaction
if (tr.isUserEvent('select')) {
const previous_selection = <EditorSelection>tr.startState.selection,
current_selection = <EditorSelection>tr.selection;
current_selection = <EditorSelection>tr.selection;

if (current_selection.main.anchor === current_selection.main.head) {
const text = tr.startState.doc.toString();
Expand Down Expand Up @@ -69,7 +71,7 @@ export const nodeCorrecter = EditorState.transactionFilter.of(tr => {
return {
changes,
selection: moveEditorCursor(current_selection, start_node.from + 3 + left_whitespace_end, -removed_characters),
}
};
}
}
}
Expand Down
89 changes: 45 additions & 44 deletions src/editor/editor-preview-buttons.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import {MarkdownView, setIcon} from "obsidian";
import type CommentatorPlugin from "../main";
import { MarkdownView, setIcon } from 'obsidian';

import type CommentatorPlugin from '../main';

export function loadEditorButtons(plugin: CommentatorPlugin) {
const status_mapping = [
{ icon: "message-square", tooltip: "Show all suggestions", label: "Showing suggestions" },
{ icon: "check", tooltip: "Preview \"accept all\"", label: "Previewing \"accept all\"" },
{ icon: "cross", tooltip: "Preview \"reject all\"" , label: "Previewing \"reject all\"" },
];

for (const leaf of app.workspace.getLeavesOfType("markdown")) {
const view = leaf.view as MarkdownView;
if (plugin.button_mapping.has(view)) continue;

const buttonElement = view.addAction("message-square", "View all suggestions", () => {
plugin.settings.suggestion_status = (plugin.settings.suggestion_status + 1) % status_mapping.length;
const { icon, tooltip, label } = status_mapping[plugin.settings.suggestion_status];
setIcon(buttonElement, icon);
buttonElement.setAttribute("aria-label", tooltip);
statusElement.innerText = label;
plugin.saveSettings();
});

const statusElement = buttonElement.createSpan({
text: status_mapping[plugin.settings.suggestion_status].label,
cls: "criticmarkup-suggestion-status"
});

// @ts-ignore (Parent element exists)
buttonElement.parentElement.insertBefore(statusElement, buttonElement);

plugin.button_mapping.set(view, {
button: buttonElement,
status: statusElement,
});
}
const status_mapping = [
{ icon: 'message-square', tooltip: 'Show all suggestions', label: 'Showing suggestions' },
{ icon: 'check', tooltip: 'Preview "accept all"', label: 'Previewing "accept all"' },
{ icon: 'cross', tooltip: 'Preview "reject all"', label: 'Previewing "reject all"' },
];

for (const leaf of app.workspace.getLeavesOfType('markdown')) {
const view = leaf.view as MarkdownView;
if (plugin.button_mapping.has(view)) continue;

const buttonElement = view.addAction('message-square', 'View all suggestions', () => {
plugin.settings.suggestion_status = (plugin.settings.suggestion_status + 1) % status_mapping.length;
const { icon, tooltip, label } = status_mapping[plugin.settings.suggestion_status];
setIcon(buttonElement, icon);
buttonElement.setAttribute('aria-label', tooltip);
statusElement.innerText = label;
plugin.saveSettings();
});

const statusElement = buttonElement.createSpan({
text: status_mapping[plugin.settings.suggestion_status].label,
cls: 'criticmarkup-suggestion-status',
});

// @ts-ignore (Parent element exists)
buttonElement.parentElement.insertBefore(statusElement, buttonElement);

plugin.button_mapping.set(view, {
button: buttonElement,
status: statusElement,
});
}
}

export async function removeEditorButtons(plugin: CommentatorPlugin) {
for (const leaf of app.workspace.getLeavesOfType("markdown")) {
const view = leaf.view as MarkdownView;
if (!plugin.button_mapping.has(view)) continue;
const elements = plugin.button_mapping.get(view);
if (elements) {
elements.button.detach();
elements.status.detach();
}
plugin.button_mapping.delete(view);
}
for (const leaf of app.workspace.getLeavesOfType('markdown')) {
const view = leaf.view as MarkdownView;
if (!plugin.button_mapping.has(view)) continue;
const elements = plugin.button_mapping.get(view);
if (elements) {
elements.button.detach();
elements.status.detach();
}
plugin.button_mapping.delete(view);
}
}
11 changes: 4 additions & 7 deletions src/editor/editor-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Editor, EditorPosition } from 'obsidian';

import type { Tree } from '@lezer/common';
import { EditorSelection } from '@codemirror/state';

Expand Down Expand Up @@ -33,11 +34,10 @@ export function moveEditorCursor(selection: EditorSelection, change_start: numbe
return EditorSelection.range(
selection.ranges[0].from + offset,
selection.ranges[0].to + offset,
)
);
}



export function selectionToRange(editor: Editor): number[] {
const selection = editor.listSelections()[0];
return [
Expand All @@ -52,19 +52,16 @@ 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')
return undefined
return undefined;
if (node.type.name === 'MSub')
return node.parent;
return node;
}



export function nodesInSelection(tree: Tree, start?: number, end?: number) {
const nodes: { from: number, middle?: number, to: number, type: string }[] = [];

Expand All @@ -84,7 +81,7 @@ export function nodesInSelection(tree: Tree, start?: number, end?: number) {
middle: node.node.nextSibling.from,
to: node.to,
type: node.type.name,
})
});
} else {
nodes.push({
from: node.from,
Expand Down
17 changes: 6 additions & 11 deletions src/editor/live-preview.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {
Decoration,
DecorationSet,
EditorView,
PluginValue,
ViewPlugin,
ViewUpdate,
} from '@codemirror/view';
import type { Extension, Range } from '@codemirror/state';
import type { Tree } from '@lezer/common';

import { criticmarkupLanguage } from './parser';
import { TreeFragment } from '@lezer/common';

import { RangeSet } from '@codemirror/state';
import type { Extension, Range } from '@codemirror/state';
import { Decoration, DecorationSet, EditorView, PluginValue, ViewPlugin, ViewUpdate } from '@codemirror/view';

import { buildMarkers, CriticMarkupMarker, gutterExtension } from './criticmarkup-gutter';

import { criticmarkupLanguage } from './parser';

import type { PluginSettings } from '../types';
import { selectionRangeOverlap } from './editor-util';

Expand Down
23 changes: 12 additions & 11 deletions src/editor/post-processor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { MarkdownView } from 'obsidian';

import { criticmarkupLanguage } from './parser';

import type {MarkdownView} from "obsidian";
import { CM_Syntax } from '../util';

export function postProcess(el: HTMLElement, ctx: any, settings: any) {
Expand All @@ -21,7 +22,7 @@ export function postProcess(el: HTMLElement, ctx: any, settings: any) {
break;
} else if (name === 'MSub') continue;

const is_rendered = output[start+1] !== CM_Syntax[name][0];
const is_rendered = output[start + 1] !== CM_Syntax[name][0];

if (name === 'Substitution') {
cursor.firstChild();
Expand Down Expand Up @@ -50,24 +51,24 @@ export function postProcess(el: HTMLElement, ctx: any, settings: any) {
let new_content = output.substring(change.start, change.end).slice(3, -3);

let new_element = '';
if (change.name === "Addition") {
if (change.name === 'Addition') {
if (!settings.suggestion_status)
new_element = `<span class='criticmarkup-inline criticmarkup-addition'>${new_content}</span>`;
else if (settings.suggestion_status === 1)
new_element = `${new_content}`;
else
new_element = ``;
} else if (change.name === "Deletion") {
} else if (change.name === 'Deletion') {
if (!settings.suggestion_status)
new_element = `<span class='criticmarkup-inline criticmarkup-deletion'>${new_content}</span>`;
else if (settings.suggestion_status === 1)
new_element = ``;
else
new_element = `${new_content}`;
} else if (change.name === "Substitution") {
} else if (change.name === 'Substitution') {
let middle = <number>change.middle - change.start + 2;
if (change.is_rendered) {
new_content = new_content.slice(3, -4)
new_content = new_content.slice(3, -4);
middle -= 3;
}

Expand All @@ -77,13 +78,13 @@ export function postProcess(el: HTMLElement, ctx: any, settings: any) {
new_element = `${new_content.substring(middle)}`;
else
new_element = `${new_content.substring(0, middle - 5)}`;
} else if (change.name === "Highlight") {
} else if (change.name === 'Highlight') {
if (change.is_rendered)
new_content = new_content.slice(4, -5)
new_content = new_content.slice(4, -5);
new_element = `<mark>${new_content}</mark>`;
} else if (change.name === "Comment") {
} else if (change.name === 'Comment') {
if (change.is_rendered)
new_content = new_content.slice(6, -6)
new_content = new_content.slice(6, -6);
new_element = `<span class='criticmarkup-comment'>${new_content}</span>`;
}

Expand All @@ -94,7 +95,7 @@ export function postProcess(el: HTMLElement, ctx: any, settings: any) {

export function postProcessorUpdate() {
// TODO: Check if this should only apply to the active editor instance
for (const leaf of app.workspace.getLeavesOfType("markdown")) {
for (const leaf of app.workspace.getLeavesOfType('markdown')) {
const view = leaf.view as MarkdownView;

const scroll_height = view.previewMode.renderer.previewEl.scrollTop;
Expand Down
Loading

0 comments on commit 4672442

Please sign in to comment.