Skip to content

Commit 4bca16a

Browse files
author
Tobias Glasmachers
committed
minor editor improvements for "paste"
1 parent a490bb0 commit 4bca16a

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/ide/editor/actions.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ export class SimpleAction extends Action {
3636
pos: number;
3737
remove: Uint32Array;
3838
insert: Uint32Array;
39+
canMerge: boolean;
3940

4041
public constructor(
4142
document: Document,
4243
pos: number,
4344
remove: number | null | Uint32Array = null,
44-
insert: null | Uint32Array = null
45+
insert: null | Uint32Array = null,
46+
canMerge: boolean = true
4547
) {
4648
super();
4749
this.pos = pos;
@@ -51,6 +53,7 @@ export class SimpleAction extends Action {
5153
: remove
5254
: Action.empty;
5355
this.insert = insert ? insert : Action.empty;
56+
this.canMerge = canMerge;
5457
}
5558

5659
public execute(document, redo) {

src/ide/editor/document.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,9 @@ export class Document {
266266
if (canMerge && this.history_pos > 0) {
267267
let prev = this.history[this.history_pos - 1];
268268
if (
269+
action instanceof SimpleAction &&
269270
prev instanceof SimpleAction &&
270-
action instanceof SimpleAction
271+
prev.canMerge
271272
) {
272273
if (
273274
action.remove.length === 0 &&

src/ide/editor/editor.ts

+23-11
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ export class Editor {
628628
let pgx = this.offscreen.width - this.charwidth;
629629
let pgy = this.offscreen.height - this.em;
630630

631+
const delay = 0;
631632
if (center) {
632633
this.scroll_x = Math.max(0, this.em * row + this.hpad - pgy / 2);
633634
this.scroll_y = Math.max(0, this.charwidth * col - pgx / 2);
@@ -636,34 +637,41 @@ export class Editor {
636637
this.scroll_x / window.devicePixelRatio;
637638
this.dom_scroller.scrollTop =
638639
this.scroll_y / window.devicePixelRatio;
639-
}, 0);
640+
}, delay);
640641
} else {
641642
if (this.em * row < this.scroll_y) {
642643
this.scroll_y = this.em * row;
643644
window.setTimeout(() => {
645+
this.scroll_y = this.em * row;
644646
this.dom_scroller.scrollTop =
645647
this.scroll_y / window.devicePixelRatio;
646-
}, 0);
648+
}, delay);
647649
} else if (this.em * row > this.scroll_y + pgy) {
648650
this.scroll_y = this.em * row - pgy;
649651
window.setTimeout(() => {
652+
this.scroll_y = this.em * row - pgy;
650653
this.dom_scroller.scrollTop =
651654
this.scroll_y / window.devicePixelRatio;
652-
}, 0);
655+
}, delay);
653656
}
654657

655658
if (this.charwidth * col - this.hpad < this.scroll_x) {
656659
this.scroll_x = Math.max(0, this.charwidth * col - this.hpad);
657660
window.setTimeout(() => {
661+
this.scroll_x = Math.max(
662+
0,
663+
this.charwidth * col - this.hpad
664+
);
658665
this.dom_scroller.scrollLeft =
659666
this.scroll_x / window.devicePixelRatio;
660-
}, 0);
667+
}, delay);
661668
} else if (this.charwidth * col + this.hpad > this.scroll_x + pgx) {
662669
this.scroll_x = this.charwidth * col + this.hpad - pgx;
663670
window.setTimeout(() => {
671+
this.scroll_x = this.charwidth * col + this.hpad - pgx;
664672
this.dom_scroller.scrollLeft =
665673
this.scroll_x / window.devicePixelRatio;
666-
}, 0);
674+
}, delay);
667675
}
668676
}
669677
}
@@ -779,7 +787,10 @@ export class Editor {
779787
}
780788

781789
// Create, execute, and add a SimpleAction.
782-
private simpleAction(ins: null | string | Uint32Array) {
790+
private simpleAction(
791+
ins: null | string | Uint32Array,
792+
canMerge: boolean = true
793+
) {
783794
if (this.readOnly) return;
784795

785796
let pos = this.document.cursor;
@@ -796,9 +807,10 @@ export class Editor {
796807
this.document,
797808
pos,
798809
rem,
799-
ins ? ins : null
810+
ins ? ins : null,
811+
canMerge
800812
);
801-
this.document.execute(action, true);
813+
this.document.execute(action, canMerge);
802814
this.scrollIntoView();
803815
if (this.eventHandlers.changed) {
804816
let { line, removed, inserted } = action.linesChanged(
@@ -862,7 +874,6 @@ export class Editor {
862874
this.document.cursor = this.document.size();
863875
} else if (bound === "undo" && !this.readOnly) {
864876
let action = this.document.undo();
865-
this.scrollIntoView();
866877
if (this.eventHandlers.changed) {
867878
if (action && action instanceof SimpleAction) {
868879
let { line, removed, inserted } = action.linesChanged(
@@ -871,9 +882,9 @@ export class Editor {
871882
this.eventHandlers.changed(line, inserted, removed); // swap of inserted and removed is intentional!
872883
} else this.eventHandlers.changed(null, null, null);
873884
}
885+
this.scrollIntoView();
874886
} else if (bound === "redo" && !this.readOnly) {
875887
let action = this.document.redo();
876-
this.scrollIntoView();
877888
if (this.eventHandlers.changed) {
878889
if (action && action instanceof SimpleAction) {
879890
let { line, removed, inserted } = action.linesChanged(
@@ -882,6 +893,7 @@ export class Editor {
882893
this.eventHandlers.changed(line, removed, inserted);
883894
} else this.eventHandlers.changed(null, null, null);
884895
}
896+
this.scrollIntoView();
885897
} else if (bound === "toggle comment" && !this.readOnly) {
886898
// toggle line comments
887899
let marker = this.document.language.linecomment;
@@ -1282,7 +1294,7 @@ export class Editor {
12821294
if (event.clipboardData && !this.readOnly) {
12831295
let s = event.clipboardData.getData("Text");
12841296
if (selected || s.length > 0) {
1285-
this.simpleAction(s);
1297+
this.simpleAction(s, false);
12861298
this.docChanged();
12871299
this.draw();
12881300
}

0 commit comments

Comments
 (0)