@j1nn0/vanilla-autokana 2.0.3 → 2.1.0

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.
@@ -1,5 +1,6 @@
1
1
  export declare class KanaExtractor {
2
2
  private static readonly EXTRACTION_PATTERN;
3
+ private static readonly CONTAINS_NON_KANA_PATTERN;
3
4
  private static readonly COMPACTING_PATTERN;
4
5
  static extract(input: string): string[];
5
6
  static compact(input: string): string;
@@ -1,5 +1,6 @@
1
1
  export declare class KanaExtractor {
2
2
  private static readonly EXTRACTION_PATTERN;
3
+ private static readonly CONTAINS_NON_KANA_PATTERN;
3
4
  private static readonly COMPACTING_PATTERN;
4
5
  static extract(input: string): string[];
5
6
  static compact(input: string): string;
package/dist/autokana.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{static{this.EXTRACTION_PATTERN=/[^  ぁあ-んゔー]/g}static{this.COMPACTING_PATTERN=/[ぁぃぅぇぉっゃゅょ]/g}static extract(e){return e.replace(this.EXTRACTION_PATTERN,``).split(``)}static compact(e){return e.replace(this.COMPACTING_PATTERN,``)}static containsNonKana(e){return e.search(this.EXTRACTION_PATTERN)!==-1}},t={ァ:`ァ`,ア:`ア`,ィ:`ィ`,イ:`イ`,ゥ:`ゥ`,ウ:`ウ`,ェ:`ェ`,エ:`エ`,ォ:`ォ`,オ:`オ`,カ:`カ`,ガ:`ガ`,キ:`キ`,ギ:`ギ`,ク:`ク`,グ:`グ`,ケ:`ケ`,ゲ:`ゲ`,コ:`コ`,ゴ:`ゴ`,サ:`サ`,ザ:`ザ`,シ:`シ`,ジ:`ジ`,ス:`ス`,ズ:`ズ`,セ:`セ`,ゼ:`ゼ`,ソ:`ソ`,ゾ:`ゾ`,タ:`タ`,ダ:`ダ`,チ:`チ`,ヂ:`ヂ`,ッ:`ッ`,ツ:`ツ`,ヅ:`ヅ`,テ:`テ`,デ:`デ`,ト:`ト`,ド:`ド`,ナ:`ナ`,ニ:`ニ`,ヌ:`ヌ`,ネ:`ネ`,ノ:`ノ`,ハ:`ハ`,バ:`バ`,パ:`パ`,ヒ:`ヒ`,ビ:`ビ`,ピ:`ピ`,フ:`フ`,ブ:`ブ`,プ:`プ`,ヘ:`ヘ`,ベ:`ベ`,ペ:`ペ`,ホ:`ホ`,ボ:`ボ`,ポ:`ポ`,マ:`マ`,ミ:`ミ`,ム:`ム`,メ:`メ`,モ:`モ`,ャ:`ャ`,ヤ:`ヤ`,ュ:`ュ`,ユ:`ユ`,ョ:`ョ`,ヨ:`ヨ`,ラ:`ラ`,リ:`リ`,ル:`ル`,レ:`レ`,ロ:`ロ`,ワ:`ワ`,ヰ:`イ`,ヱ:`エ`,ヲ:`ヲ`,ヺ:`ヺ`,ン:`ン`,ヴ:`ヴ`,ー:`ー`,"。":`。`,"、":`、`},n=12353,r=12436,i=12445,a=12446;function o(e){return e>=n&&e<=r||e===i||e===a}var s=class{static toKatakana(e,n){if(n===`hiragana`)return e;let r,i=``;for(let t=0;t<e.length;t+=1)r=e.charCodeAt(t),o(r)?i+=String.fromCharCode(r+96):i+=e.charAt(t);return n===`half`?i.replace(/[ァ-ヴヺー。、]/g,e=>t[e]??e):i}};function c(e){return typeof e==`string`?`"${e}"`:`the provided element`}function l(e){if(typeof e==`string`){if(!/^[[.#:]/.test(e))return document.getElementById(e);try{return document.querySelector(e)}catch{throw Error(`AutoKana: Invalid selector for ${c(e)}.`)}}return e instanceof HTMLElement?e:null}function u(e){return e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement}function d(e){let t=l(e);if(!t){let t=c(e);throw Error(`AutoKana: Element not found for ${t}. Ensure the DOM element exists before calling bind(). For Vue/React, call bind() inside onMounted()/useEffect() or after the component is mounted.`)}if(!u(t)){let t=c(e);throw Error(`AutoKana: Element must be an input or textarea for ${t}.`)}return t}function f(e){if(e===void 0||e===``)return;let t=l(e);if(t){if(!u(t))throw Error(`AutoKana: Element must be an input or textarea for ${c(e)}.`);return t}}var p=class{constructor(e,t=``,n={}){this.blurHandler=()=>{this.debug(`blur`),this.isComposing=!1},this.focusHandler=()=>{this.debug(`focus`),this.elFurigana&&(this.committedKana=this.elFurigana.value),this.isComposing=!1,this.pendingKana=[],this.lastNewInput=``,this.previousRawInput=``,this.lastConvertedInput=this.elName.value,this.processValue()},this.compositionStartHandler=()=>{this.debug(`compositionstart`),this.isComposing=!0},this.compositionEndHandler=()=>{this.debug(`compositionend`),this.isComposing=!1,this.lastNewInput=``,this.previousRawInput=``,this.processValue()},this.inputHandler=e=>{this.debug(`input`,e.isComposing),this.processValue()},this.isActive=!0,this.committedKana=``,this.furigana=``,this.isComposing=!1,this.lastConvertedInput=``,this.lastNewInput=``,this.pendingKana=[],this.previousRawInput=``,this.option=Object.assign({katakana:`hiragana`,debug:!1},n);let r=d(e),i=f(t);this.elName=r,i&&(this.elFurigana=i),this.registerEvents(this.elName)}getFurigana(){return this.furigana}start(){this.isActive=!0}stop(){this.isActive=!1}toggle(e){e?this.isActive=e.target.checked:this.isActive=!this.isActive}reset(){this.committedKana=``,this.furigana=``,this.isComposing=!1,this.lastConvertedInput=``,this.lastNewInput=``,this.pendingKana=[],this.previousRawInput=``}initializeValues(){this.reset()}registerEvents(e){e.addEventListener(`blur`,this.blurHandler),e.addEventListener(`focus`,this.focusHandler),e.addEventListener(`compositionstart`,this.compositionStartHandler),e.addEventListener(`compositionend`,this.compositionEndHandler),e.addEventListener(`input`,this.inputHandler)}setFurigana(e=!1){if(this.isActive){let t=s.toKatakana(this.committedKana+this.pendingKana.join(``),this.option.katakana),n=this.option.katakana===`half`?t.replace(/ /g,` `):t;if(!e&&n===this.furigana)return;this.furigana=n,this.elFurigana&&(this.elFurigana.value=this.furigana,this.elFurigana.dispatchEvent(new Event(`input`,{bubbles:!0}))),this.option.onChange&&this.option.onChange(this.furigana)}}extractNewInput(e){if(e.indexOf(this.lastConvertedInput)!==-1)return e.replace(this.lastConvertedInput,``);let t=this.lastConvertedInput.split(``),n=e.split(``);for(let e=0;e<t.length;e+=1)t[e]===n[e]&&(n[e]=``);return n.join(``)}detectAndCommitConversion(t){if(Math.abs(this.pendingKana.length-t.length)>1){let n=this.pendingKana.join(``),r=t.join(``);if(!r.startsWith(n)){let t=e.compact(r).split(``);Math.abs(this.pendingKana.length-t.length)>1&&this.commitPendingKana()}}else this.pendingKana.length===this.lastNewInput.length&&this.pendingKana.join(``)!==this.lastNewInput&&e.containsNonKana(this.lastNewInput)&&this.commitPendingKana()}handleCompositionInput(t){let n=e.extract(t);n.length>=this.pendingKana.length&&(this.pendingKana=n),this.setFurigana()}handleNormalInput(t,n){if(this.lastNewInput===t)return;let r=n.length<this.previousRawInput.length;this.lastNewInput=t,this.previousRawInput=n;let i=e.extract(t);if(!r){let e=this.committedKana;if(this.detectAndCommitConversion(i),this.committedKana!==e){this.lastConvertedInput=n,this.setFurigana();return}}this.pendingKana=i,this.setFurigana()}processValue(){let e=this.elName.value;if(e===``){this.reset(),this.setFurigana(!0);return}let t=this.extractNewInput(e);if(this.isComposing){this.handleCompositionInput(t);return}this.handleNormalInput(t,e)}commitPendingKana(){this.committedKana+=this.pendingKana.join(``),this.pendingKana=[]}destroy(){this.elName.removeEventListener(`blur`,this.blurHandler),this.elName.removeEventListener(`focus`,this.focusHandler),this.elName.removeEventListener(`compositionstart`,this.compositionStartHandler),this.elName.removeEventListener(`compositionend`,this.compositionEndHandler),this.elName.removeEventListener(`input`,this.inputHandler),this.elName=null,this.elFurigana=void 0}debug(...e){this.option.debug&&console.log(...e)}};function m(e,t,n={}){return new p(e,t,n)}exports.AutoKana=p,exports.KanaConverter=s,exports.KanaExtractor=e,exports.bind=m;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{static{this.EXTRACTION_PATTERN=/[^  ぁあ-んゔー]/g}static{this.CONTAINS_NON_KANA_PATTERN=/[^  ぁあ-んゔー]/}static{this.COMPACTING_PATTERN=/[ぁぃぅぇぉっゃゅょ]/g}static extract(e){return e.replace(this.EXTRACTION_PATTERN,``).split(``)}static compact(e){return e.replace(this.COMPACTING_PATTERN,``)}static containsNonKana(e){return e.search(this.CONTAINS_NON_KANA_PATTERN)!==-1}},t={ァ:`ァ`,ア:`ア`,ィ:`ィ`,イ:`イ`,ゥ:`ゥ`,ウ:`ウ`,ェ:`ェ`,エ:`エ`,ォ:`ォ`,オ:`オ`,カ:`カ`,ガ:`ガ`,キ:`キ`,ギ:`ギ`,ク:`ク`,グ:`グ`,ケ:`ケ`,ゲ:`ゲ`,コ:`コ`,ゴ:`ゴ`,サ:`サ`,ザ:`ザ`,シ:`シ`,ジ:`ジ`,ス:`ス`,ズ:`ズ`,セ:`セ`,ゼ:`ゼ`,ソ:`ソ`,ゾ:`ゾ`,タ:`タ`,ダ:`ダ`,チ:`チ`,ヂ:`ヂ`,ッ:`ッ`,ツ:`ツ`,ヅ:`ヅ`,テ:`テ`,デ:`デ`,ト:`ト`,ド:`ド`,ナ:`ナ`,ニ:`ニ`,ヌ:`ヌ`,ネ:`ネ`,ノ:`ノ`,ハ:`ハ`,バ:`バ`,パ:`パ`,ヒ:`ヒ`,ビ:`ビ`,ピ:`ピ`,フ:`フ`,ブ:`ブ`,プ:`プ`,ヘ:`ヘ`,ベ:`ベ`,ペ:`ペ`,ホ:`ホ`,ボ:`ボ`,ポ:`ポ`,マ:`マ`,ミ:`ミ`,ム:`ム`,メ:`メ`,モ:`モ`,ャ:`ャ`,ヤ:`ヤ`,ュ:`ュ`,ユ:`ユ`,ョ:`ョ`,ヨ:`ヨ`,ラ:`ラ`,リ:`リ`,ル:`ル`,レ:`レ`,ロ:`ロ`,ワ:`ワ`,ヰ:`イ`,ヱ:`エ`,ヲ:`ヲ`,ヺ:`ヺ`,ン:`ン`,ヴ:`ヴ`,ー:`ー`,"。":`。`,"、":`、`},n=12353,r=12436,i=12445,a=12446;function o(e){return e>=n&&e<=r||e===i||e===a}var s=class{static toKatakana(e,n){if(n===`hiragana`)return e;let r=``;for(let i=0;i<e.length;i+=1){let a=e.charCodeAt(i),s=o(a)?String.fromCharCode(a+96):e.charAt(i);r+=n===`half`?t[s]??s:s}return r}};function c(e){return typeof e==`string`?`"${e}"`:`the provided element`}function l(e){if(typeof e==`string`){if(!/^[[.#:]/.test(e))return document.getElementById(e);try{return document.querySelector(e)}catch{throw Error(`AutoKana: Invalid selector for ${c(e)}.`)}}return e instanceof HTMLElement?e:null}function u(e){return e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement}function d(e){let t=l(e);if(!t){let t=c(e);throw Error(`AutoKana: Element not found for ${t}. Ensure the DOM element exists before calling bind(). For Vue/React, call bind() inside onMounted()/useEffect() or after the component is mounted.`)}if(!u(t)){let t=c(e);throw Error(`AutoKana: Element must be an input or textarea for ${t}.`)}return t}function f(e){if(e===void 0||e===``)return;let t=l(e);if(t){if(!u(t))throw Error(`AutoKana: Element must be an input or textarea for ${c(e)}.`);return t}}var p=class{constructor(e,t=``,n={}){this.blurHandler=()=>{this.option.debug&&this.debug(`blur`),this.isComposing=!1},this.focusHandler=()=>{this.option.debug&&this.debug(`focus`),this.elFurigana&&(this.committedKana=this.elFurigana.value),this.isComposing=!1,this.pendingKana=[],this.lastNewInput=``,this.previousRawInput=``,this.lastConvertedInput=this.elName.value,this.processValue()},this.compositionStartHandler=()=>{this.option.debug&&this.debug(`compositionstart`),this.isComposing=!0},this.compositionEndHandler=()=>{this.option.debug&&this.debug(`compositionend`),this.isComposing=!1,this.lastNewInput=``,this.previousRawInput=``,this.processValue()},this.inputHandler=e=>{this.option.debug&&this.debug(`input`,e.isComposing),this.processValue()},this.isActive=!0,this.committedKana=``,this.furigana=``,this.isComposing=!1,this.lastConvertedInput=``,this.lastNewInput=``,this.pendingKana=[],this.previousRawInput=``,this.option=Object.assign({katakana:`hiragana`,debug:!1},n);let r=d(e),i=f(t);this.elName=r,i&&(this.elFurigana=i),this.registerEvents(this.elName)}getFurigana(){return this.furigana}start(){this.isActive=!0}stop(){this.isActive=!1}toggle(e){e?this.isActive=e.target.checked:this.isActive=!this.isActive}reset(){this.committedKana=``,this.furigana=``,this.isComposing=!1,this.lastConvertedInput=``,this.lastNewInput=``,this.pendingKana=[],this.previousRawInput=``}initializeValues(){this.reset()}registerEvents(e){e.addEventListener(`blur`,this.blurHandler),e.addEventListener(`focus`,this.focusHandler),e.addEventListener(`compositionstart`,this.compositionStartHandler),e.addEventListener(`compositionend`,this.compositionEndHandler),e.addEventListener(`input`,this.inputHandler)}setFurigana(e=!1){if(this.isActive){let t=s.toKatakana(this.committedKana+this.pendingKana.join(``),this.option.katakana),n=this.option.katakana===`half`&&t.indexOf(` `)!==-1?t.replace(/ /g,` `):t;if(!e&&n===this.furigana)return;this.furigana=n,this.elFurigana&&(this.elFurigana.value=this.furigana,this.elFurigana.dispatchEvent(new Event(`input`,{bubbles:!0}))),this.option.onChange&&this.option.onChange(this.furigana)}}extractNewInput(e){let t=e.indexOf(this.lastConvertedInput);if(t!==-1)return e.slice(0,t)+e.slice(t+this.lastConvertedInput.length);let n=``;for(let t=0;t<e.length;t+=1)this.lastConvertedInput.charCodeAt(t)!==e.charCodeAt(t)&&(n+=e.charAt(t));return n}detectAndCommitConversion(t){if(Math.abs(this.pendingKana.length-t.length)>1){let n=this.pendingKana.join(``),r=t.join(``);if(!r.startsWith(n)){let t=e.compact(r).split(``);Math.abs(this.pendingKana.length-t.length)>1&&this.commitPendingKana()}}else this.pendingKana.length===this.lastNewInput.length&&this.pendingKana.join(``)!==this.lastNewInput&&e.containsNonKana(this.lastNewInput)&&this.commitPendingKana()}handleCompositionInput(t){let n=e.extract(t);n.length>=this.pendingKana.length&&(this.pendingKana=n),this.setFurigana()}handleNormalInput(t,n){if(this.lastNewInput===t)return;let r=n.length<this.previousRawInput.length;this.lastNewInput=t,this.previousRawInput=n;let i=e.extract(t);if(!r){let e=this.committedKana;if(this.detectAndCommitConversion(i),this.committedKana!==e){this.lastConvertedInput=n,this.setFurigana();return}}this.pendingKana=i,this.setFurigana()}processValue(){let e=this.elName.value;if(e===``){this.reset(),this.setFurigana(!0);return}let t=this.extractNewInput(e);if(this.isComposing){this.handleCompositionInput(t);return}this.handleNormalInput(t,e)}commitPendingKana(){this.committedKana+=this.pendingKana.join(``),this.pendingKana=[]}destroy(){this.elName.removeEventListener(`blur`,this.blurHandler),this.elName.removeEventListener(`focus`,this.focusHandler),this.elName.removeEventListener(`compositionstart`,this.compositionStartHandler),this.elName.removeEventListener(`compositionend`,this.compositionEndHandler),this.elName.removeEventListener(`input`,this.inputHandler),this.elName=null,this.elFurigana=void 0}debug(e,t){if(this.option.debug){if(t===void 0){console.log(e);return}console.log(e,t)}}};function m(e,t,n={}){return new p(e,t,n)}exports.AutoKana=p,exports.KanaConverter=s,exports.KanaExtractor=e,exports.bind=m;
@@ -3,6 +3,9 @@ var e = class {
3
3
  static {
4
4
  this.EXTRACTION_PATTERN = /[^  ぁあ-んゔー]/g;
5
5
  }
6
+ static {
7
+ this.CONTAINS_NON_KANA_PATTERN = /[^  ぁあ-んゔー]/;
8
+ }
6
9
  static {
7
10
  this.COMPACTING_PATTERN = /[ぁぃぅぇぉっゃゅょ]/g;
8
11
  }
@@ -13,7 +16,7 @@ var e = class {
13
16
  return e.replace(this.COMPACTING_PATTERN, "");
14
17
  }
15
18
  static containsNonKana(e) {
16
- return e.search(this.EXTRACTION_PATTERN) !== -1;
19
+ return e.search(this.CONTAINS_NON_KANA_PATTERN) !== -1;
17
20
  }
18
21
  }, t = {
19
22
  ァ: "ァ",
@@ -110,9 +113,12 @@ function o(e) {
110
113
  var s = class {
111
114
  static toKatakana(e, n) {
112
115
  if (n === "hiragana") return e;
113
- let r, i = "";
114
- for (let t = 0; t < e.length; t += 1) r = e.charCodeAt(t), o(r) ? i += String.fromCharCode(r + 96) : i += e.charAt(t);
115
- return n === "half" ? i.replace(/[ァ-ヴヺー。、]/g, (e) => t[e] ?? e) : i;
116
+ let r = "";
117
+ for (let i = 0; i < e.length; i += 1) {
118
+ let a = e.charCodeAt(i), s = o(a) ? String.fromCharCode(a + 96) : e.charAt(i);
119
+ r += n === "half" ? t[s] ?? s : s;
120
+ }
121
+ return r;
116
122
  }
117
123
  };
118
124
  //#endregion
@@ -159,15 +165,15 @@ function f(e) {
159
165
  var p = class {
160
166
  constructor(e, t = "", n = {}) {
161
167
  this.blurHandler = () => {
162
- this.debug("blur"), this.isComposing = !1;
168
+ this.option.debug && this.debug("blur"), this.isComposing = !1;
163
169
  }, this.focusHandler = () => {
164
- this.debug("focus"), this.elFurigana && (this.committedKana = this.elFurigana.value), this.isComposing = !1, this.pendingKana = [], this.lastNewInput = "", this.previousRawInput = "", this.lastConvertedInput = this.elName.value, this.processValue();
170
+ this.option.debug && this.debug("focus"), this.elFurigana && (this.committedKana = this.elFurigana.value), this.isComposing = !1, this.pendingKana = [], this.lastNewInput = "", this.previousRawInput = "", this.lastConvertedInput = this.elName.value, this.processValue();
165
171
  }, this.compositionStartHandler = () => {
166
- this.debug("compositionstart"), this.isComposing = !0;
172
+ this.option.debug && this.debug("compositionstart"), this.isComposing = !0;
167
173
  }, this.compositionEndHandler = () => {
168
- this.debug("compositionend"), this.isComposing = !1, this.lastNewInput = "", this.previousRawInput = "", this.processValue();
174
+ this.option.debug && this.debug("compositionend"), this.isComposing = !1, this.lastNewInput = "", this.previousRawInput = "", this.processValue();
169
175
  }, this.inputHandler = (e) => {
170
- this.debug("input", e.isComposing), this.processValue();
176
+ this.option.debug && this.debug("input", e.isComposing), this.processValue();
171
177
  }, this.isActive = !0, this.committedKana = "", this.furigana = "", this.isComposing = !1, this.lastConvertedInput = "", this.lastNewInput = "", this.pendingKana = [], this.previousRawInput = "", this.option = Object.assign({
172
178
  katakana: "hiragana",
173
179
  debug: !1
@@ -198,16 +204,17 @@ var p = class {
198
204
  }
199
205
  setFurigana(e = !1) {
200
206
  if (this.isActive) {
201
- let t = s.toKatakana(this.committedKana + this.pendingKana.join(""), this.option.katakana), n = this.option.katakana === "half" ? t.replace(/ /g, " ") : t;
207
+ let t = s.toKatakana(this.committedKana + this.pendingKana.join(""), this.option.katakana), n = this.option.katakana === "half" && t.indexOf(" ") !== -1 ? t.replace(/ /g, " ") : t;
202
208
  if (!e && n === this.furigana) return;
203
209
  this.furigana = n, this.elFurigana && (this.elFurigana.value = this.furigana, this.elFurigana.dispatchEvent(new Event("input", { bubbles: !0 }))), this.option.onChange && this.option.onChange(this.furigana);
204
210
  }
205
211
  }
206
212
  extractNewInput(e) {
207
- if (e.indexOf(this.lastConvertedInput) !== -1) return e.replace(this.lastConvertedInput, "");
208
- let t = this.lastConvertedInput.split(""), n = e.split("");
209
- for (let e = 0; e < t.length; e += 1) t[e] === n[e] && (n[e] = "");
210
- return n.join("");
213
+ let t = e.indexOf(this.lastConvertedInput);
214
+ if (t !== -1) return e.slice(0, t) + e.slice(t + this.lastConvertedInput.length);
215
+ let n = "";
216
+ for (let t = 0; t < e.length; t += 1) this.lastConvertedInput.charCodeAt(t) !== e.charCodeAt(t) && (n += e.charAt(t));
217
+ return n;
211
218
  }
212
219
  detectAndCommitConversion(t) {
213
220
  if (Math.abs(this.pendingKana.length - t.length) > 1) {
@@ -255,8 +262,14 @@ var p = class {
255
262
  destroy() {
256
263
  this.elName.removeEventListener("blur", this.blurHandler), this.elName.removeEventListener("focus", this.focusHandler), this.elName.removeEventListener("compositionstart", this.compositionStartHandler), this.elName.removeEventListener("compositionend", this.compositionEndHandler), this.elName.removeEventListener("input", this.inputHandler), this.elName = null, this.elFurigana = void 0;
257
264
  }
258
- debug(...e) {
259
- this.option.debug && console.log(...e);
265
+ debug(e, t) {
266
+ if (this.option.debug) {
267
+ if (t === void 0) {
268
+ console.log(e);
269
+ return;
270
+ }
271
+ console.log(e, t);
272
+ }
260
273
  }
261
274
  };
262
275
  //#endregion
@@ -1 +1 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.AutoKana={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class{static{this.EXTRACTION_PATTERN=/[^  ぁあ-んゔー]/g}static{this.COMPACTING_PATTERN=/[ぁぃぅぇぉっゃゅょ]/g}static extract(e){return e.replace(this.EXTRACTION_PATTERN,``).split(``)}static compact(e){return e.replace(this.COMPACTING_PATTERN,``)}static containsNonKana(e){return e.search(this.EXTRACTION_PATTERN)!==-1}},n={ァ:`ァ`,ア:`ア`,ィ:`ィ`,イ:`イ`,ゥ:`ゥ`,ウ:`ウ`,ェ:`ェ`,エ:`エ`,ォ:`ォ`,オ:`オ`,カ:`カ`,ガ:`ガ`,キ:`キ`,ギ:`ギ`,ク:`ク`,グ:`グ`,ケ:`ケ`,ゲ:`ゲ`,コ:`コ`,ゴ:`ゴ`,サ:`サ`,ザ:`ザ`,シ:`シ`,ジ:`ジ`,ス:`ス`,ズ:`ズ`,セ:`セ`,ゼ:`ゼ`,ソ:`ソ`,ゾ:`ゾ`,タ:`タ`,ダ:`ダ`,チ:`チ`,ヂ:`ヂ`,ッ:`ッ`,ツ:`ツ`,ヅ:`ヅ`,テ:`テ`,デ:`デ`,ト:`ト`,ド:`ド`,ナ:`ナ`,ニ:`ニ`,ヌ:`ヌ`,ネ:`ネ`,ノ:`ノ`,ハ:`ハ`,バ:`バ`,パ:`パ`,ヒ:`ヒ`,ビ:`ビ`,ピ:`ピ`,フ:`フ`,ブ:`ブ`,プ:`プ`,ヘ:`ヘ`,ベ:`ベ`,ペ:`ペ`,ホ:`ホ`,ボ:`ボ`,ポ:`ポ`,マ:`マ`,ミ:`ミ`,ム:`ム`,メ:`メ`,モ:`モ`,ャ:`ャ`,ヤ:`ヤ`,ュ:`ュ`,ユ:`ユ`,ョ:`ョ`,ヨ:`ヨ`,ラ:`ラ`,リ:`リ`,ル:`ル`,レ:`レ`,ロ:`ロ`,ワ:`ワ`,ヰ:`イ`,ヱ:`エ`,ヲ:`ヲ`,ヺ:`ヺ`,ン:`ン`,ヴ:`ヴ`,ー:`ー`,"。":`。`,"、":`、`},r=12353,i=12436,a=12445,o=12446;function s(e){return e>=r&&e<=i||e===a||e===o}var c=class{static toKatakana(e,t){if(t===`hiragana`)return e;let r,i=``;for(let t=0;t<e.length;t+=1)r=e.charCodeAt(t),s(r)?i+=String.fromCharCode(r+96):i+=e.charAt(t);return t===`half`?i.replace(/[ァ-ヴヺー。、]/g,e=>n[e]??e):i}};function l(e){return typeof e==`string`?`"${e}"`:`the provided element`}function u(e){if(typeof e==`string`){if(!/^[[.#:]/.test(e))return document.getElementById(e);try{return document.querySelector(e)}catch{throw Error(`AutoKana: Invalid selector for ${l(e)}.`)}}return e instanceof HTMLElement?e:null}function d(e){return e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement}function f(e){let t=u(e);if(!t){let t=l(e);throw Error(`AutoKana: Element not found for ${t}. Ensure the DOM element exists before calling bind(). For Vue/React, call bind() inside onMounted()/useEffect() or after the component is mounted.`)}if(!d(t)){let t=l(e);throw Error(`AutoKana: Element must be an input or textarea for ${t}.`)}return t}function p(e){if(e===void 0||e===``)return;let t=u(e);if(t){if(!d(t))throw Error(`AutoKana: Element must be an input or textarea for ${l(e)}.`);return t}}var m=class{constructor(e,t=``,n={}){this.blurHandler=()=>{this.debug(`blur`),this.isComposing=!1},this.focusHandler=()=>{this.debug(`focus`),this.elFurigana&&(this.committedKana=this.elFurigana.value),this.isComposing=!1,this.pendingKana=[],this.lastNewInput=``,this.previousRawInput=``,this.lastConvertedInput=this.elName.value,this.processValue()},this.compositionStartHandler=()=>{this.debug(`compositionstart`),this.isComposing=!0},this.compositionEndHandler=()=>{this.debug(`compositionend`),this.isComposing=!1,this.lastNewInput=``,this.previousRawInput=``,this.processValue()},this.inputHandler=e=>{this.debug(`input`,e.isComposing),this.processValue()},this.isActive=!0,this.committedKana=``,this.furigana=``,this.isComposing=!1,this.lastConvertedInput=``,this.lastNewInput=``,this.pendingKana=[],this.previousRawInput=``,this.option=Object.assign({katakana:`hiragana`,debug:!1},n);let r=f(e),i=p(t);this.elName=r,i&&(this.elFurigana=i),this.registerEvents(this.elName)}getFurigana(){return this.furigana}start(){this.isActive=!0}stop(){this.isActive=!1}toggle(e){e?this.isActive=e.target.checked:this.isActive=!this.isActive}reset(){this.committedKana=``,this.furigana=``,this.isComposing=!1,this.lastConvertedInput=``,this.lastNewInput=``,this.pendingKana=[],this.previousRawInput=``}initializeValues(){this.reset()}registerEvents(e){e.addEventListener(`blur`,this.blurHandler),e.addEventListener(`focus`,this.focusHandler),e.addEventListener(`compositionstart`,this.compositionStartHandler),e.addEventListener(`compositionend`,this.compositionEndHandler),e.addEventListener(`input`,this.inputHandler)}setFurigana(e=!1){if(this.isActive){let t=c.toKatakana(this.committedKana+this.pendingKana.join(``),this.option.katakana),n=this.option.katakana===`half`?t.replace(/ /g,` `):t;if(!e&&n===this.furigana)return;this.furigana=n,this.elFurigana&&(this.elFurigana.value=this.furigana,this.elFurigana.dispatchEvent(new Event(`input`,{bubbles:!0}))),this.option.onChange&&this.option.onChange(this.furigana)}}extractNewInput(e){if(e.indexOf(this.lastConvertedInput)!==-1)return e.replace(this.lastConvertedInput,``);let t=this.lastConvertedInput.split(``),n=e.split(``);for(let e=0;e<t.length;e+=1)t[e]===n[e]&&(n[e]=``);return n.join(``)}detectAndCommitConversion(e){if(Math.abs(this.pendingKana.length-e.length)>1){let n=this.pendingKana.join(``),r=e.join(``);if(!r.startsWith(n)){let e=t.compact(r).split(``);Math.abs(this.pendingKana.length-e.length)>1&&this.commitPendingKana()}}else this.pendingKana.length===this.lastNewInput.length&&this.pendingKana.join(``)!==this.lastNewInput&&t.containsNonKana(this.lastNewInput)&&this.commitPendingKana()}handleCompositionInput(e){let n=t.extract(e);n.length>=this.pendingKana.length&&(this.pendingKana=n),this.setFurigana()}handleNormalInput(e,n){if(this.lastNewInput===e)return;let r=n.length<this.previousRawInput.length;this.lastNewInput=e,this.previousRawInput=n;let i=t.extract(e);if(!r){let e=this.committedKana;if(this.detectAndCommitConversion(i),this.committedKana!==e){this.lastConvertedInput=n,this.setFurigana();return}}this.pendingKana=i,this.setFurigana()}processValue(){let e=this.elName.value;if(e===``){this.reset(),this.setFurigana(!0);return}let t=this.extractNewInput(e);if(this.isComposing){this.handleCompositionInput(t);return}this.handleNormalInput(t,e)}commitPendingKana(){this.committedKana+=this.pendingKana.join(``),this.pendingKana=[]}destroy(){this.elName.removeEventListener(`blur`,this.blurHandler),this.elName.removeEventListener(`focus`,this.focusHandler),this.elName.removeEventListener(`compositionstart`,this.compositionStartHandler),this.elName.removeEventListener(`compositionend`,this.compositionEndHandler),this.elName.removeEventListener(`input`,this.inputHandler),this.elName=null,this.elFurigana=void 0}debug(...e){this.option.debug&&console.log(...e)}};function h(e,t,n={}){return new m(e,t,n)}e.AutoKana=m,e.KanaConverter=c,e.KanaExtractor=t,e.bind=h});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.AutoKana={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class{static{this.EXTRACTION_PATTERN=/[^  ぁあ-んゔー]/g}static{this.CONTAINS_NON_KANA_PATTERN=/[^  ぁあ-んゔー]/}static{this.COMPACTING_PATTERN=/[ぁぃぅぇぉっゃゅょ]/g}static extract(e){return e.replace(this.EXTRACTION_PATTERN,``).split(``)}static compact(e){return e.replace(this.COMPACTING_PATTERN,``)}static containsNonKana(e){return e.search(this.CONTAINS_NON_KANA_PATTERN)!==-1}},n={ァ:`ァ`,ア:`ア`,ィ:`ィ`,イ:`イ`,ゥ:`ゥ`,ウ:`ウ`,ェ:`ェ`,エ:`エ`,ォ:`ォ`,オ:`オ`,カ:`カ`,ガ:`ガ`,キ:`キ`,ギ:`ギ`,ク:`ク`,グ:`グ`,ケ:`ケ`,ゲ:`ゲ`,コ:`コ`,ゴ:`ゴ`,サ:`サ`,ザ:`ザ`,シ:`シ`,ジ:`ジ`,ス:`ス`,ズ:`ズ`,セ:`セ`,ゼ:`ゼ`,ソ:`ソ`,ゾ:`ゾ`,タ:`タ`,ダ:`ダ`,チ:`チ`,ヂ:`ヂ`,ッ:`ッ`,ツ:`ツ`,ヅ:`ヅ`,テ:`テ`,デ:`デ`,ト:`ト`,ド:`ド`,ナ:`ナ`,ニ:`ニ`,ヌ:`ヌ`,ネ:`ネ`,ノ:`ノ`,ハ:`ハ`,バ:`バ`,パ:`パ`,ヒ:`ヒ`,ビ:`ビ`,ピ:`ピ`,フ:`フ`,ブ:`ブ`,プ:`プ`,ヘ:`ヘ`,ベ:`ベ`,ペ:`ペ`,ホ:`ホ`,ボ:`ボ`,ポ:`ポ`,マ:`マ`,ミ:`ミ`,ム:`ム`,メ:`メ`,モ:`モ`,ャ:`ャ`,ヤ:`ヤ`,ュ:`ュ`,ユ:`ユ`,ョ:`ョ`,ヨ:`ヨ`,ラ:`ラ`,リ:`リ`,ル:`ル`,レ:`レ`,ロ:`ロ`,ワ:`ワ`,ヰ:`イ`,ヱ:`エ`,ヲ:`ヲ`,ヺ:`ヺ`,ン:`ン`,ヴ:`ヴ`,ー:`ー`,"。":`。`,"、":`、`},r=12353,i=12436,a=12445,o=12446;function s(e){return e>=r&&e<=i||e===a||e===o}var c=class{static toKatakana(e,t){if(t===`hiragana`)return e;let r=``;for(let i=0;i<e.length;i+=1){let a=e.charCodeAt(i),o=s(a)?String.fromCharCode(a+96):e.charAt(i);r+=t===`half`?n[o]??o:o}return r}};function l(e){return typeof e==`string`?`"${e}"`:`the provided element`}function u(e){if(typeof e==`string`){if(!/^[[.#:]/.test(e))return document.getElementById(e);try{return document.querySelector(e)}catch{throw Error(`AutoKana: Invalid selector for ${l(e)}.`)}}return e instanceof HTMLElement?e:null}function d(e){return e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement}function f(e){let t=u(e);if(!t){let t=l(e);throw Error(`AutoKana: Element not found for ${t}. Ensure the DOM element exists before calling bind(). For Vue/React, call bind() inside onMounted()/useEffect() or after the component is mounted.`)}if(!d(t)){let t=l(e);throw Error(`AutoKana: Element must be an input or textarea for ${t}.`)}return t}function p(e){if(e===void 0||e===``)return;let t=u(e);if(t){if(!d(t))throw Error(`AutoKana: Element must be an input or textarea for ${l(e)}.`);return t}}var m=class{constructor(e,t=``,n={}){this.blurHandler=()=>{this.option.debug&&this.debug(`blur`),this.isComposing=!1},this.focusHandler=()=>{this.option.debug&&this.debug(`focus`),this.elFurigana&&(this.committedKana=this.elFurigana.value),this.isComposing=!1,this.pendingKana=[],this.lastNewInput=``,this.previousRawInput=``,this.lastConvertedInput=this.elName.value,this.processValue()},this.compositionStartHandler=()=>{this.option.debug&&this.debug(`compositionstart`),this.isComposing=!0},this.compositionEndHandler=()=>{this.option.debug&&this.debug(`compositionend`),this.isComposing=!1,this.lastNewInput=``,this.previousRawInput=``,this.processValue()},this.inputHandler=e=>{this.option.debug&&this.debug(`input`,e.isComposing),this.processValue()},this.isActive=!0,this.committedKana=``,this.furigana=``,this.isComposing=!1,this.lastConvertedInput=``,this.lastNewInput=``,this.pendingKana=[],this.previousRawInput=``,this.option=Object.assign({katakana:`hiragana`,debug:!1},n);let r=f(e),i=p(t);this.elName=r,i&&(this.elFurigana=i),this.registerEvents(this.elName)}getFurigana(){return this.furigana}start(){this.isActive=!0}stop(){this.isActive=!1}toggle(e){e?this.isActive=e.target.checked:this.isActive=!this.isActive}reset(){this.committedKana=``,this.furigana=``,this.isComposing=!1,this.lastConvertedInput=``,this.lastNewInput=``,this.pendingKana=[],this.previousRawInput=``}initializeValues(){this.reset()}registerEvents(e){e.addEventListener(`blur`,this.blurHandler),e.addEventListener(`focus`,this.focusHandler),e.addEventListener(`compositionstart`,this.compositionStartHandler),e.addEventListener(`compositionend`,this.compositionEndHandler),e.addEventListener(`input`,this.inputHandler)}setFurigana(e=!1){if(this.isActive){let t=c.toKatakana(this.committedKana+this.pendingKana.join(``),this.option.katakana),n=this.option.katakana===`half`&&t.indexOf(` `)!==-1?t.replace(/ /g,` `):t;if(!e&&n===this.furigana)return;this.furigana=n,this.elFurigana&&(this.elFurigana.value=this.furigana,this.elFurigana.dispatchEvent(new Event(`input`,{bubbles:!0}))),this.option.onChange&&this.option.onChange(this.furigana)}}extractNewInput(e){let t=e.indexOf(this.lastConvertedInput);if(t!==-1)return e.slice(0,t)+e.slice(t+this.lastConvertedInput.length);let n=``;for(let t=0;t<e.length;t+=1)this.lastConvertedInput.charCodeAt(t)!==e.charCodeAt(t)&&(n+=e.charAt(t));return n}detectAndCommitConversion(e){if(Math.abs(this.pendingKana.length-e.length)>1){let n=this.pendingKana.join(``),r=e.join(``);if(!r.startsWith(n)){let e=t.compact(r).split(``);Math.abs(this.pendingKana.length-e.length)>1&&this.commitPendingKana()}}else this.pendingKana.length===this.lastNewInput.length&&this.pendingKana.join(``)!==this.lastNewInput&&t.containsNonKana(this.lastNewInput)&&this.commitPendingKana()}handleCompositionInput(e){let n=t.extract(e);n.length>=this.pendingKana.length&&(this.pendingKana=n),this.setFurigana()}handleNormalInput(e,n){if(this.lastNewInput===e)return;let r=n.length<this.previousRawInput.length;this.lastNewInput=e,this.previousRawInput=n;let i=t.extract(e);if(!r){let e=this.committedKana;if(this.detectAndCommitConversion(i),this.committedKana!==e){this.lastConvertedInput=n,this.setFurigana();return}}this.pendingKana=i,this.setFurigana()}processValue(){let e=this.elName.value;if(e===``){this.reset(),this.setFurigana(!0);return}let t=this.extractNewInput(e);if(this.isComposing){this.handleCompositionInput(t);return}this.handleNormalInput(t,e)}commitPendingKana(){this.committedKana+=this.pendingKana.join(``),this.pendingKana=[]}destroy(){this.elName.removeEventListener(`blur`,this.blurHandler),this.elName.removeEventListener(`focus`,this.focusHandler),this.elName.removeEventListener(`compositionstart`,this.compositionStartHandler),this.elName.removeEventListener(`compositionend`,this.compositionEndHandler),this.elName.removeEventListener(`input`,this.inputHandler),this.elName=null,this.elFurigana=void 0}debug(e,t){if(this.option.debug){if(t===void 0){console.log(e);return}console.log(e,t)}}};function h(e,t,n={}){return new m(e,t,n)}e.AutoKana=m,e.KanaConverter=c,e.KanaExtractor=t,e.bind=h});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@j1nn0/vanilla-autokana",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "A JavaScript library to complete furigana automatically (forked from ryo-utsunomiya/vanilla-autokana).",
5
5
  "author": "j1nn0 <j1nn0.github@gmail.com>",
6
6
  "contributors": [