Skip to content

Commit a4b9d2d

Browse files
authored
Fix: wrong type of paste event and missing types for themelist extension (#5725)
* fix: wrong type of paste event * Apply suggestions from code review * generate with typescript 5.7.3 * fix: small bug in `moveToMouse`; types * add types for themelist
1 parent 9a76656 commit a4b9d2d

File tree

7 files changed

+55
-13
lines changed

7 files changed

+55
-13
lines changed

ace-internal.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ export namespace Ace {
511511
/**
512512
* Emitted when text is pasted.
513513
**/
514-
"paste": (text: string, event: any) => void;
514+
"paste": (e: { text: string, event?: ClipboardEvent }) => void;
515515
/**
516516
* Emitted when the selection style changes, via [[Editor.setSelectionStyle]].
517517
* @param data Contains one property, `data`, which indicates the new selection style
@@ -530,6 +530,7 @@ export namespace Ace {
530530
"gutterclick": (e: MouseEvent) => void;
531531
"showGutterTooltip": (e: GutterTooltip) => void;
532532
"hideGutterTooltip": (e: GutterTooltip) => void;
533+
"compositionStart": () => void;
533534
}
534535

535536
interface AcePopupEvents {
@@ -1334,6 +1335,7 @@ declare module "./src/editor" {
13341335
showSettingsMenu?: () => void,
13351336
searchBox?: Ace.SearchBox,
13361337
_eventRegistry?: any,
1338+
$textInputAriaLabel?: string
13371339
}
13381340
}
13391341

ace.d.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,10 @@ declare module "ace-code" {
422422
/**
423423
* Emitted when text is pasted.
424424
**/
425-
"paste": (text: string, event: any) => void;
425+
"paste": (e: {
426+
text: string;
427+
event?: ClipboardEvent;
428+
}) => void;
426429
/**
427430
* Emitted when the selection style changes, via [[Editor.setSelectionStyle]].
428431
* @param data Contains one property, `data`, which indicates the new selection style
@@ -443,6 +446,7 @@ declare module "ace-code" {
443446
"gutterclick": (e: MouseEvent) => void;
444447
"showGutterTooltip": (e: GutterTooltip) => void;
445448
"hideGutterTooltip": (e: GutterTooltip) => void;
449+
"compositionStart": () => void;
446450
}
447451
interface AcePopupEvents {
448452
"click": (e: MouseEvent) => void;

demo/test_package/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {MarkerGroup, MarkerGroupItem} from "ace-code/src/marker_group";
99
import {HoverTooltip} from "ace-code/src/tooltip";
1010
import {hardWrap} from "ace-code/src/ext/hardwrap";
1111
import {SearchBox} from "ace-code/src/ext/searchbox";
12+
import {themesByName} from 'ace-code/src/ext/themelist';
1213

1314
import("ace-code/src/ext/language_tools");
1415
import "../../src/test/mockdom.js";
@@ -134,4 +135,13 @@ const filter = new FilteredList([]);
134135
filter.setFilter("test");
135136

136137
editor.session.startOperation();
137-
editor.session.endOperation();
138+
editor.session.endOperation();
139+
140+
editor.on("paste", (e) => {
141+
var htmlString = e.event?.clipboardData?.getData("text/html")
142+
if (htmlString) {
143+
e.text = htmlString
144+
}
145+
})
146+
147+
themesByName.textmate?.theme;

src/editor.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ class Editor {
853853
/**
854854
* Called whenever a text "paste" happens.
855855
* @param {String} text The pasted text
856-
* @param {any} event
856+
* @param {ClipboardEvent} [event]
857857
* @internal
858858
**/
859859
onPaste(text, event) {
@@ -863,7 +863,7 @@ class Editor {
863863

864864
/**
865865
*
866-
* @param e
866+
* @param {string | {text: string, event?: ClipboardEvent}} e
867867
* @returns {boolean}
868868
*/
869869
$handlePaste(e) {

src/ext/themelist.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
"use strict";
1010

11+
/**
12+
* @typedef {Object} Theme
13+
* @property {string} caption - The display caption of the theme.
14+
* @property {string} theme - The path or identifier for the ACE theme.
15+
* @property {boolean} isDark - Indicates whether the theme is dark or light.
16+
* @property {string} name - The normalized name used as the key.
17+
*/
18+
1119
var themeData = [
1220
["Chrome" ],
1321
["Clouds" ],
@@ -54,11 +62,14 @@ var themeData = [
5462
["CloudEditor Dark" ,"cloud_editor_dark" , "dark"]
5563
];
5664

57-
65+
/**
66+
* @type {Object<string, Theme>}
67+
*/
5868
exports.themesByName = {};
5969

6070
/**
6171
* An array containing information about available themes.
72+
* @type {Theme[]}
6273
*/
6374
exports.themes = themeData.map(function(data) {
6475
var name = data[1] || data[0].replace(/ /g, "_").toLowerCase();

src/keyboard/textinput.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var valueResetRegex = isIOS ? /\s/ : /\n/;
2222
var isMobile = useragent.isMobile;
2323

2424
var TextInput;
25-
TextInput= function(parentNode, host) {
25+
TextInput= function(/**@type{HTMLTextAreaElement} */parentNode, /**@type{import("../editor").Editor} */host) {
2626
/**@type {HTMLTextAreaElement & {msGetInputContext?: () => {compositionStartOffset: number}, getInputContext?: () => {compositionStartOffset: number}}}*/
2727
var text = dom.createElement("textarea");
2828
text.className = "ace_text-input";
@@ -35,7 +35,7 @@ TextInput= function(parentNode, host) {
3535
text.style.opacity = "0";
3636
parentNode.insertBefore(text, parentNode.firstChild);
3737

38-
var copied = false;
38+
/**@type{boolean|string}*/var copied = false;
3939
var pasted = false;
4040
/**@type {(boolean|Object) & {context?: any, useTextareaForIME?: boolean, selectionStart?: number, markerRange?: any}}} */
4141
var inComposition = false;
@@ -62,7 +62,7 @@ TextInput= function(parentNode, host) {
6262

6363
// Set number of extra lines in textarea, some screenreaders
6464
// perform better with extra lines above and below in the textarea.
65-
this.setNumberOfExtraLines = function(number) {
65+
this.setNumberOfExtraLines = function(/**@type{number}*/number) {
6666
rowStart = Number.MAX_SAFE_INTEGER;
6767
rowEnd = Number.MIN_SAFE_INTEGER;
6868

@@ -695,7 +695,7 @@ TextInput= function(parentNode, host) {
695695
var rect = host.container.getBoundingClientRect();
696696
var style = dom.computedStyle(host.container);
697697
var top = rect.top + (parseInt(style.borderTopWidth) || 0);
698-
var left = rect.left + (parseInt(rect.borderLeftWidth) || 0);
698+
var left = rect.left + (parseInt(style.borderLeftWidth) || 0);
699699
var maxTop = rect.bottom - top - text.clientHeight -2;
700700
var move = function(e) {
701701
dom.translate(text, e.clientX - left - 2, Math.min(e.clientY - top - 2, maxTop));

types/ace-ext.d.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,28 @@ declare module "ace-code/src/ext/modelist" {
404404
}
405405
}
406406
declare module "ace-code/src/ext/themelist" {
407-
export const themesByName: {};
408-
export const themes: {
407+
export const themesByName: {
408+
[x: string]: Theme;
409+
};
410+
export const themes: Theme[];
411+
export type Theme = {
412+
/**
413+
* - The display caption of the theme.
414+
*/
409415
caption: string;
416+
/**
417+
* - The path or identifier for the ACE theme.
418+
*/
410419
theme: string;
420+
/**
421+
* - Indicates whether the theme is dark or light.
422+
*/
411423
isDark: boolean;
424+
/**
425+
* - The normalized name used as the key.
426+
*/
412427
name: string;
413-
}[];
428+
};
414429
}
415430
declare module "ace-code/src/ext/options" {
416431
export class OptionPanel {

0 commit comments

Comments
 (0)