pi-paster 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +6 -5
- package/package.json +2 -2
- package/src/editor.ts +8 -5
package/dist/index.d.mts
CHANGED
|
@@ -157,7 +157,7 @@ declare class AttachmentStore {
|
|
|
157
157
|
//#region src/editor.d.ts
|
|
158
158
|
declare const PASTE_START = "\u001B[200~";
|
|
159
159
|
declare const PASTE_END = "\u001B[201~";
|
|
160
|
-
declare function segmentTextWithAtomicImages(text: string, store: AttachmentStore, validPasteIds?: Set<number
|
|
160
|
+
declare function segmentTextWithAtomicImages(text: string, store: AttachmentStore, validPasteIds?: Set<number>, segmenter?: Intl.Segmenter): Intl.SegmentData[];
|
|
161
161
|
declare class PasterEditor extends CustomEditor {
|
|
162
162
|
private readonly pasterKeybindings;
|
|
163
163
|
private readonly pasterOptions;
|
package/dist/index.mjs
CHANGED
|
@@ -883,7 +883,8 @@ const PASTE_START = "\x1B[200~";
|
|
|
883
883
|
const PASTE_END = "\x1B[201~";
|
|
884
884
|
const PLACEHOLDER_REGEX = /\[#image \d+\]/g;
|
|
885
885
|
const PASTE_MARKER_REGEX = /\[paste #(\d+)( (\+\d+ lines|\d+ chars))?\]/g;
|
|
886
|
-
const
|
|
886
|
+
const graphemeSegmenter = new Intl.Segmenter(void 0, { granularity: "grapheme" });
|
|
887
|
+
const wordSegmenter = new Intl.Segmenter(void 0, { granularity: "word" });
|
|
887
888
|
function atomicSpansForText(text, validPasteIds) {
|
|
888
889
|
const spans = [];
|
|
889
890
|
for (const match of text.matchAll(PASTE_MARKER_REGEX)) {
|
|
@@ -903,12 +904,12 @@ function atomicSpansForText(text, validPasteIds) {
|
|
|
903
904
|
}
|
|
904
905
|
return spans.sort((a, b) => a.start - b.start || a.end - b.end);
|
|
905
906
|
}
|
|
906
|
-
function segmentTextWithAtomicImages(text, store, validPasteIds = /* @__PURE__ */ new Set()) {
|
|
907
|
+
function segmentTextWithAtomicImages(text, store, validPasteIds = /* @__PURE__ */ new Set(), segmenter = graphemeSegmenter) {
|
|
907
908
|
const spans = atomicSpansForText(text, validPasteIds);
|
|
908
|
-
if (spans.length === 0) return [...
|
|
909
|
+
if (spans.length === 0) return [...segmenter.segment(text)];
|
|
909
910
|
const result = [];
|
|
910
911
|
let spanIndex = 0;
|
|
911
|
-
for (const segment of
|
|
912
|
+
for (const segment of segmenter.segment(text)) {
|
|
912
913
|
while (spanIndex < spans.length && spans[spanIndex].end <= segment.index) spanIndex++;
|
|
913
914
|
const span = spans[spanIndex];
|
|
914
915
|
if (span && segment.index >= span.start && segment.index < span.end) {
|
|
@@ -984,7 +985,7 @@ var PasterEditor = class extends CustomEditor {
|
|
|
984
985
|
}
|
|
985
986
|
installAtomicImageSegmentation() {
|
|
986
987
|
const editor = this;
|
|
987
|
-
editor.segment = (text) => segmentTextWithAtomicImages(text, this.pasterOptions.store, new Set(editor.pastes?.keys() ?? []));
|
|
988
|
+
editor.segment = (text, mode = "grapheme") => segmentTextWithAtomicImages(text, this.pasterOptions.store, new Set(editor.pastes?.keys() ?? []), mode === "word" ? wordSegmenter : graphemeSegmenter);
|
|
988
989
|
}
|
|
989
990
|
async handlePasteClipboardImage() {
|
|
990
991
|
const attachment = await this.pasterOptions.pasteClipboardImage?.();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-paster",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Pi extension that turns pasted image paths into first-class image attachments.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"image-attachments",
|
|
@@ -58,6 +58,6 @@
|
|
|
58
58
|
"extensions": [
|
|
59
59
|
"./src/index.ts"
|
|
60
60
|
],
|
|
61
|
-
"image": "https://unpkg.com/pi-paster@0.2.
|
|
61
|
+
"image": "https://unpkg.com/pi-paster@0.2.3/docs/preview.png"
|
|
62
62
|
}
|
|
63
63
|
}
|
package/src/editor.ts
CHANGED
|
@@ -8,7 +8,8 @@ export const PASTE_START = "\x1b[200~";
|
|
|
8
8
|
export const PASTE_END = "\x1b[201~";
|
|
9
9
|
const PLACEHOLDER_REGEX = /\[#image \d+\]/g;
|
|
10
10
|
const PASTE_MARKER_REGEX = /\[paste #(\d+)( (\+\d+ lines|\d+ chars))?\]/g;
|
|
11
|
-
const
|
|
11
|
+
const graphemeSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
12
|
+
const wordSegmenter = new Intl.Segmenter(undefined, { granularity: "word" });
|
|
12
13
|
|
|
13
14
|
interface AtomicSpan {
|
|
14
15
|
start: number;
|
|
@@ -16,7 +17,7 @@ interface AtomicSpan {
|
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
interface EditorSegmentationAccess {
|
|
19
|
-
segment?: (text: string) => Iterable<Intl.SegmentData>;
|
|
20
|
+
segment?: (text: string, mode?: "word" | "grapheme") => Iterable<Intl.SegmentData>;
|
|
20
21
|
pastes?: Map<number, string>;
|
|
21
22
|
}
|
|
22
23
|
|
|
@@ -41,13 +42,14 @@ export function segmentTextWithAtomicImages(
|
|
|
41
42
|
text: string,
|
|
42
43
|
store: AttachmentStore,
|
|
43
44
|
validPasteIds: Set<number> = new Set(),
|
|
45
|
+
segmenter: Intl.Segmenter = graphemeSegmenter,
|
|
44
46
|
): Intl.SegmentData[] {
|
|
45
47
|
const spans = atomicSpansForText(text, validPasteIds);
|
|
46
|
-
if (spans.length === 0) return [...
|
|
48
|
+
if (spans.length === 0) return [...segmenter.segment(text)];
|
|
47
49
|
|
|
48
50
|
const result: Intl.SegmentData[] = [];
|
|
49
51
|
let spanIndex = 0;
|
|
50
|
-
for (const segment of
|
|
52
|
+
for (const segment of segmenter.segment(text)) {
|
|
51
53
|
while (spanIndex < spans.length && spans[spanIndex]!.end <= segment.index) spanIndex++;
|
|
52
54
|
const span = spans[spanIndex];
|
|
53
55
|
if (span && segment.index >= span.start && segment.index < span.end) {
|
|
@@ -159,11 +161,12 @@ export class PasterEditor extends CustomEditor {
|
|
|
159
161
|
|
|
160
162
|
private installAtomicImageSegmentation(): void {
|
|
161
163
|
const editor = this as unknown as EditorSegmentationAccess;
|
|
162
|
-
editor.segment = (text: string) =>
|
|
164
|
+
editor.segment = (text: string, mode: "word" | "grapheme" = "grapheme") =>
|
|
163
165
|
segmentTextWithAtomicImages(
|
|
164
166
|
text,
|
|
165
167
|
this.pasterOptions.store,
|
|
166
168
|
new Set(editor.pastes?.keys() ?? []),
|
|
169
|
+
mode === "word" ? wordSegmenter : graphemeSegmenter,
|
|
167
170
|
);
|
|
168
171
|
}
|
|
169
172
|
|