Skip to content

Commit

Permalink
feat!: using Antidote public API instead of old private API
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop support of Connectix 10. Antidote 10 users now needs to install Connectix 11 to use Antidote 10 with this plugin (are mainly concerned the users of Linux).
  • Loading branch information
Heziode committed Jan 12, 2023
2 parents 69ef022 + 8e550c9 commit 446dd0e
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 1,209 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Antidote supports these languages:
- English
- French

This plugin works with Antidote 10 and higher, on macOS, Linux and Windows.
This plugin works with Antidote 10 (using Connectix version 11 or higher) and higher, and Antidote web, on macOS, Linux and Windows.

**This plugin does not work on smartphones.**

Expand All @@ -31,9 +31,15 @@ This plugin adds 3 icons into the status bar:

![](./assets/obsidian-statusbar.png)

You can show or hide every icons from settings.
You can show or hide every icon from settings.

It also add command for corrector, dictionary and guide.
It also adds command for corrector, dictionary and guide.

### Additional note for Linux users

Antidote 10 is the latest supported version of Antidote on this OS. Accordingly, the Connectix version 10 installed with this version is not supported.

In order to use Antidote 10 (or web) with this plugin, you will have to install the latest Connectix (version 11 or higher). Connectix is the bridge between Obsidian and Antidote (10, 11+, web), so by keeping Antidote 10 with Connectix 11, you will be able to use this plugin.

## Support this plugin

Expand Down
109 changes: 9 additions & 100 deletions src/ObsidianTexteurAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createHash } from 'crypto';
import { parse as parseURL } from 'url';

import { EditorView } from '@codemirror/view';
Expand All @@ -14,8 +13,6 @@ import {
AgentTexteur,
ZoneDeTexte,
} from './lib/antidote/InterfaceAgentTexteur';
import { Range, Selection } from './lib/vscode/rangeUtils';
import { getWordRangeAtPosition } from './lib/vscode/textUtils';

export class AgentTexteurAPI extends AgentTexteur {
private edView: EditorView;
Expand All @@ -37,71 +34,14 @@ export class AgentTexteurAPI extends AgentTexteur {
return this.mdView.editor.posToOffset(pos);
}

private PositionVS(pos: number): EditorPosition {
private PositionObsidian(pos: number): EditorPosition {
return this.mdView.editor.offsetToPos(pos);
}

DonneRetourDeCharriot(): string {
return this.edView.state.lineBreak;
}

async DonneSelectionDansSonContexte(): Promise<{
texte: string;
debutSelection: number;
finSelection: number;
}> {
let selectedText = '';
let startOffset = 0;
let endOffset = 0;

let range: Range | undefined;
const selection: Selection = Selection.ofSel(
this.mdView.editor.listSelections()[0]
);
const sentenceRegex = new RegExp('[^.!?]*[.!?]');
const startSentenceRange = getWordRangeAtPosition(
this.mdView,
selection.start,
sentenceRegex
);
const endSentenceRange = getWordRangeAtPosition(
this.mdView,
selection.end,
sentenceRegex
);

if (endSentenceRange) {
range = startSentenceRange?.union(endSentenceRange);
}

selectedText = range
? this.mdView.editor.getRange(
range.start.toEditorPosition(),
range.end.toEditorPosition()
)
: this.mdView.editor.getValue();
startOffset =
this.PositionAbsolue(selection.start.toEditorPosition()) -
this.PositionAbsolue(
range
? range.start.toEditorPosition()
: selection.start.toEditorPosition()
);
endOffset =
this.PositionAbsolue(selection.end.toEditorPosition()) -
this.PositionAbsolue(
range ? range.end.toEditorPosition() : selection.end.toEditorPosition()
);

return new Promise((resolve) =>
resolve({
texte: selectedText,
debutSelection: startOffset,
finSelection: endOffset,
})
);
}

DonneTitreDocument(): string {
return this.mdView.file.name;
}
Expand Down Expand Up @@ -136,19 +76,6 @@ export class AgentTexteurAPI extends AgentTexteur {
return new Promise<ZoneDeTexte[]>((resolve) => resolve(lesZones));
}

DonneIdWSExpediteur() {
const hash = createHash('md5');
const id = new Date().getTime().toString();

hash.update(this.DonneTitreDocument());
hash.update(id);
return 'vsc' + hash.digest('hex');
}

DonneNomExpediteur() {
return 'ConnecteurVsCode';
}

PeutCorriger(
_leIDZone: string,
debut: number,
Expand All @@ -157,15 +84,15 @@ export class AgentTexteurAPI extends AgentTexteur {
): boolean {
if (!this.DocEstDisponible()) return false;

const posDebut: EditorPosition = this.PositionVS(debut);
let posFin: EditorPosition = this.PositionVS(fin);
const posDebut: EditorPosition = this.PositionObsidian(debut);
let posFin: EditorPosition = this.PositionObsidian(fin);

const contexteMatchParfaitement =
this.mdView.editor.getRange(posDebut, posFin) == laChaineOrig;
let contexteMatchAuDebut = true;

if (!contexteMatchParfaitement) {
posFin = this.PositionVS(fin + 1);
posFin = this.PositionObsidian(fin + 1);
contexteMatchAuDebut = this.mdView.editor
.getRange(posDebut, posFin)
.startsWith(laChaineOrig);
Expand All @@ -187,10 +114,11 @@ export class AgentTexteurAPI extends AgentTexteur {
leDebut: number,
laFin: number,
laChaine: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_automatique: boolean
): Promise<boolean> {
const posDebut: EditorPosition = this.PositionVS(leDebut);
const posFin: EditorPosition = this.PositionVS(laFin);
const posDebut: EditorPosition = this.PositionObsidian(leDebut);
const posFin: EditorPosition = this.PositionObsidian(laFin);
return new Promise<boolean>((resolve) => {
this.mdView.editor.replaceRange(laChaine, posDebut, posFin);
resolve(true);
Expand Down Expand Up @@ -225,27 +153,8 @@ export class AgentTexteurAPI extends AgentTexteur {
SelectionneIntervalle(_leIDZone: string, debut: number, fin: number): void {
this.MetsFocusSurLeDocument();
this.mdView.editor.setSelection(
this.PositionVS(debut),
this.PositionVS(fin)
);
}

DocEstMort(): boolean {
return !this.DocEstDisponible();
}
DonneDebutSelection(): Promise<number> {
const selection: Selection = Selection.ofSel(
this.mdView.editor.listSelections()[0]
this.PositionObsidian(debut),
this.PositionObsidian(fin)
);
return new Promise((resolve) => {
resolve(this.PositionAbsolue(selection.start.toEditorPosition()));
});
}
RemplaceMot(valeur: string): Promise<void> {
this.mdView.editor.replaceSelection(valeur);

return new Promise((resolve) => {
resolve();
});
}
}
Loading

0 comments on commit 446dd0e

Please sign in to comment.