@textcomplete/core
Advanced tools
Comparing version 0.1.9 to 0.1.10
@@ -31,3 +31,3 @@ import { EventEmitter } from "eventemitter3"; | ||
private activeIndex; | ||
static create<T>(option: DropdownOption): Dropdown; | ||
static create(option: DropdownOption): Dropdown; | ||
private constructor(); | ||
@@ -34,0 +34,0 @@ /** |
@@ -41,3 +41,3 @@ "use strict"; | ||
render(searchResults, cursorOffset) { | ||
const event = utils_1.createCustomEvent("render", { cancelable: true }); | ||
const event = (0, utils_1.createCustomEvent)("render", { cancelable: true }); | ||
this.emit("render", event); | ||
@@ -59,3 +59,3 @@ if (event.defaultPrevented) | ||
.activate(0); | ||
this.emit("rendered", utils_1.createCustomEvent("rendered")); | ||
this.emit("rendered", (0, utils_1.createCustomEvent)("rendered")); | ||
return this; | ||
@@ -77,3 +77,3 @@ } | ||
const detail = { searchResult: item.searchResult }; | ||
const event = utils_1.createCustomEvent("select", { cancelable: true, detail }); | ||
const event = (0, utils_1.createCustomEvent)("select", { cancelable: true, detail }); | ||
this.emit("select", event); | ||
@@ -83,3 +83,3 @@ if (event.defaultPrevented) | ||
this.hide(); | ||
this.emit("selected", utils_1.createCustomEvent("selected", { detail })); | ||
this.emit("selected", (0, utils_1.createCustomEvent)("selected", { detail })); | ||
return this; | ||
@@ -95,3 +95,3 @@ } | ||
if (!this.shown) { | ||
const event = utils_1.createCustomEvent("show", { cancelable: true }); | ||
const event = (0, utils_1.createCustomEvent)("show", { cancelable: true }); | ||
this.emit("show", event); | ||
@@ -102,3 +102,3 @@ if (event.defaultPrevented) | ||
this.shown = true; | ||
this.emit("shown", utils_1.createCustomEvent("shown")); | ||
this.emit("shown", (0, utils_1.createCustomEvent)("shown")); | ||
} | ||
@@ -115,3 +115,3 @@ return this; | ||
if (this.shown) { | ||
const event = utils_1.createCustomEvent("hide", { cancelable: true }); | ||
const event = (0, utils_1.createCustomEvent)("hide", { cancelable: true }); | ||
this.emit("hide", event); | ||
@@ -123,3 +123,3 @@ if (event.defaultPrevented) | ||
this.clear(); | ||
this.emit("hidden", utils_1.createCustomEvent("hidden")); | ||
this.emit("hidden", (0, utils_1.createCustomEvent)("hidden")); | ||
} | ||
@@ -126,0 +126,0 @@ return this; |
@@ -42,3 +42,3 @@ "use strict"; | ||
emitMoveEvent(code) { | ||
const moveEvent = utils_1.createCustomEvent("move", { | ||
const moveEvent = (0, utils_1.createCustomEvent)("move", { | ||
cancelable: true, | ||
@@ -59,3 +59,3 @@ detail: { | ||
emitEnterEvent() { | ||
const enterEvent = utils_1.createCustomEvent("enter", { cancelable: true }); | ||
const enterEvent = (0, utils_1.createCustomEvent)("enter", { cancelable: true }); | ||
this.emit("enter", enterEvent); | ||
@@ -71,3 +71,3 @@ return enterEvent; | ||
emitChangeEvent() { | ||
const changeEvent = utils_1.createCustomEvent("change", { | ||
const changeEvent = (0, utils_1.createCustomEvent)("change", { | ||
detail: { | ||
@@ -87,3 +87,3 @@ beforeCursor: this.getBeforeCursor(), | ||
emitEscEvent() { | ||
const escEvent = utils_1.createCustomEvent("esc", { cancelable: true }); | ||
const escEvent = (0, utils_1.createCustomEvent)("esc", { cancelable: true }); | ||
this.emit("esc", escEvent); | ||
@@ -90,0 +90,0 @@ return escEvent; |
@@ -10,3 +10,3 @@ "use strict"; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
@@ -13,0 +13,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
@@ -7,2 +7,8 @@ import { Strategy } from "./Strategy"; | ||
constructor(data: T, term: string, strategy: Strategy<T>); | ||
getReplacementData(beforeCursor: string): { | ||
start: number; | ||
end: number; | ||
beforeCursor: string; | ||
afterCursor: string; | ||
} | null; | ||
replace(beforeCursor: string, afterCursor: string): [string, string] | void; | ||
@@ -9,0 +15,0 @@ render(): string; |
@@ -12,8 +12,9 @@ "use strict"; | ||
} | ||
replace(beforeCursor, afterCursor) { | ||
getReplacementData(beforeCursor) { | ||
let result = this.strategy.replace(this.data); | ||
if (result == null) | ||
return; | ||
return null; | ||
let afterCursor = ""; | ||
if (Array.isArray(result)) { | ||
afterCursor = result[1] + afterCursor; | ||
afterCursor = result[1]; | ||
result = result[0]; | ||
@@ -23,11 +24,23 @@ } | ||
if (match == null || match.index == null) | ||
return; | ||
return null; | ||
const replacement = result | ||
.replace(MAIN, match[0]) | ||
.replace(PLACE, (_, p) => match[parseInt(p)]); | ||
return { | ||
start: match.index, | ||
end: match.index + match[0].length, | ||
beforeCursor: replacement, | ||
afterCursor: afterCursor, | ||
}; | ||
} | ||
replace(beforeCursor, afterCursor) { | ||
const replacement = this.getReplacementData(beforeCursor); | ||
if (replacement === null) | ||
return; | ||
afterCursor = replacement.afterCursor + afterCursor; | ||
return [ | ||
[ | ||
beforeCursor.slice(0, match.index), | ||
replacement, | ||
beforeCursor.slice(match.index + match[0].length), | ||
beforeCursor.slice(0, replacement.start), | ||
replacement.beforeCursor, | ||
beforeCursor.slice(replacement.end), | ||
].join(""), | ||
@@ -34,0 +47,0 @@ afterCursor, |
@@ -5,3 +5,3 @@ "use strict"; | ||
const isCustomEventSupported = typeof window !== "undefined" && !!window.CustomEvent; | ||
exports.createCustomEvent = (type, options) => { | ||
const createCustomEvent = (type, options) => { | ||
if (isCustomEventSupported) | ||
@@ -14,2 +14,3 @@ return new CustomEvent(type, options); | ||
}; | ||
exports.createCustomEvent = createCustomEvent; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@textcomplete/core", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Textcomplete core.", | ||
@@ -22,21 +22,6 @@ "main": "dist/index.js", | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.0", | ||
"@typescript-eslint/eslint-plugin": "^3.2.0", | ||
"@typescript-eslint/parser": "^3.2.0", | ||
"eslint": "^7.2.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-prettier": "^3.1.3", | ||
"jest": "^26.0.1", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^26.1.0", | ||
"typescript": "^3.9.5" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"prettier": { | ||
"semi": false | ||
}, | ||
"gitHead": "67a13ff0c065121c646ec0bf4ca76182c5a76073" | ||
"gitHead": "1199fcac0b13aaece2e145d4f133a2ca07f8fa14" | ||
} |
@@ -39,3 +39,3 @@ import { EventEmitter } from "eventemitter3" | ||
static create<T>(option: DropdownOption): Dropdown { | ||
static create(option: DropdownOption): Dropdown { | ||
const ul = document.createElement("ul") | ||
@@ -42,0 +42,0 @@ ul.className = option.className || DEFAULT_DROPDOWN_CLASS_NAME |
@@ -13,19 +13,42 @@ import { Strategy } from "./Strategy" | ||
replace(beforeCursor: string, afterCursor: string): [string, string] | void { | ||
getReplacementData(beforeCursor: string): { | ||
start: number | ||
end: number | ||
beforeCursor: string | ||
afterCursor: string | ||
} | null { | ||
let result = this.strategy.replace(this.data) | ||
if (result == null) return | ||
if (result == null) return null | ||
let afterCursor = "" | ||
if (Array.isArray(result)) { | ||
afterCursor = result[1] + afterCursor | ||
afterCursor = result[1] | ||
result = result[0] | ||
} | ||
const match = this.strategy.match(beforeCursor) | ||
if (match == null || match.index == null) return | ||
if (match == null || match.index == null) return null | ||
const replacement = result | ||
.replace(MAIN, match[0]) | ||
.replace(PLACE, (_, p) => match[parseInt(p)]) | ||
return { | ||
start: match.index, | ||
end: match.index + match[0].length, | ||
beforeCursor: replacement, | ||
afterCursor: afterCursor, | ||
} | ||
} | ||
replace(beforeCursor: string, afterCursor: string): [string, string] | void { | ||
const replacement = this.getReplacementData(beforeCursor) | ||
if (replacement === null) return | ||
afterCursor = replacement.afterCursor + afterCursor | ||
return [ | ||
[ | ||
beforeCursor.slice(0, match.index), | ||
replacement, | ||
beforeCursor.slice(match.index + match[0].length), | ||
beforeCursor.slice(0, replacement.start), | ||
replacement.beforeCursor, | ||
beforeCursor.slice(replacement.end), | ||
].join(""), | ||
@@ -32,0 +55,0 @@ afterCursor, |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import { createCustomEvent } from "./utils" | ||
@@ -2,0 +6,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
85389
0
1891