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

Use Enter to apply #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
63 changes: 41 additions & 22 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Editor, Modal, Notice, Plugin, Setting, sanitizeHTMLToDom } from 'obsidian';
import { App, Editor, Modal, EditorTransaction, Plugin, Setting, TextComponent, sanitizeHTMLToDom } from 'obsidian';
import { replacements, combiningmarks, subsuperscripts } from './data.ts';

export default class LaTeXtoUnicode extends Plugin {
Expand All @@ -7,11 +7,8 @@ export default class LaTeXtoUnicode extends Plugin {
id: "latex-to-unicode",
name: "LaTeX to Unicode",
editorCallback: (editor: Editor) => {

const onSubmit = (res: string) => {
const pos = editor.getCursor();
editor.replaceRange(`${res}`, pos);
editor.setCursor(pos);
insertRange(editor,res)
};

new LaTeXToUnicodeModal(this.app, onSubmit).open();
Expand All @@ -20,9 +17,18 @@ export default class LaTeXtoUnicode extends Plugin {
}
}

function insertRange(editor : Editor, text : string)
{
var pos = editor.getCursor()
var offset = editor.posToOffset(pos);

editor.replaceRange(text, pos); // Replace (cursor will NOT be pushed by the length of the text)

editor.setCursor(editor.offsetToPos(offset+text.length)) // Go forward to the end of the replace, for the "insert" effect
}

export class LaTeXToUnicodeModal extends Modal {
res: string;
res: string = "";

onSubmit: (res: string) => void;

Expand All @@ -38,22 +44,35 @@ export class LaTeXToUnicodeModal extends Modal {
const { contentEl } = this;

contentEl.createEl("h1", { text: "LaTeX to Unicode" });

const elContainer = contentEl.createEl('table');
elContainer.className = 'latexInputTable';
const first = elContainer.createEl('tr');
first.createEl('td', {text: 'LaTeX command'});
const elInput = first.createEl('td').createEl('input', {type: 'text'});
elInput.id = 'userInput';
elInput.addEventListener("input", (e) => {
this.res = replace(e.target.value);
document.getElementById('resDisp').value = this.res;
});
const second = elContainer.createEl('tr');
second.createEl('td', {text: 'Result'});
const elRes = second.createEl('td').createEl('input', {type: 'text'});
elRes.id = 'resDisp';
elRes.readOnly = true;
type Action = () => {}
var valueChanged : Action

new Setting(contentEl)
.setName("LaTeX command")
.addText((text)=>
text.onChange((value) => {
this.res = replace(value)
valueChanged?.()
})
.inputEl
.addEventListener('keyup',
(e : KeyboardEvent) => {
if(e.key == "Enter")
{
this.onSubmit(this.res)
this.close()
}
})
)



new Setting(contentEl)
.setName("Result")
.addText((text) => {
text.setDisabled(true)
valueChanged = () => text.setValue(this.res)
})

new Setting(contentEl).addButton((btn) =>
btn
Expand Down