Codebase list mozc / d2e045e chrome / skk / kanji_mode.js
d2e045e

Tree @d2e045e (Download .tar.gz)

kanji_mode.js @d2e045eraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright 2010-2012, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/**
 * @fileoverview This file contains Kanji preedit/conversion modes.
 */

'use strict';

console.assert(
    skk.mode, 'ime_mode_interface.js must be loaded prior to this module');

/**
 * Initializes preedit mode.
 * @constructor
 * @implements {skk.mode.InputModeInterface}
 * @param {skk.IME} ime IME instance.
 */
skk.mode.Preedit = function(ime) {
  this.mark_ = '\u25bd';  // '▽'; default preedit mark
  this.ime_ = ime;
  this.preedit_ = '';
  this.composer_ = new skk.Composer(skk.Composer.map.hiragana);
  this.waitingDeterminant_ = false;
};

/**
 * Sets symbol character that will appear in head of preedit.
 * @param {string} mark Symbol character.
 */
skk.mode.Preedit.prototype.setPreeditMark = function(mark) {
  this.mark_ = mark;
};

/**
 * @inheritDoc
 */
skk.mode.Preedit.prototype.reset = function() {
  console.debug('skk.mode.Preedit.reset');
  this.ime_.leaveCurrentMode();
};

/**
 * @inheritDoc
 */
skk.mode.Preedit.prototype.enter = function() {
  console.debug('skk.mode.Preedit.enter');
};

/**
 * @inheritDoc
 */
skk.mode.Preedit.prototype.leave = function() {
  console.debug('skk.mode.Preedit.leave');
  this.preedit_ = '';
  this.composer_.reset();
  skk.util.showPreedit(this.ime_.context.contextID, this.preedit_);
};

/**
 * @inheritDoc
 */
skk.mode.Preedit.prototype.resume = function() {
  console.debug('skk.mode.Preedit.resume');
  skk.util.showPreedit(
      this.ime_.context.contextID,
      this.mark_ + this.preedit_ + this.composer_.undetermined);
};

/**
 * @inheritDoc
 */
skk.mode.Preedit.prototype.suspend = function() {
  console.debug('skk.mode.Preedit.suspend');
  skk.util.showPreedit(this.ime_.context.contextID, '');
};

/**
 * @return {boolean} Returns true if the mode has preedit text, otherwise false.
 */
skk.mode.Preedit.prototype.hasPreedit = function() {
  console.debug('skk.mode.Preedit.hasPreedit');
  return this.preedit_ !== '';
};

/**
 * Removes last input preedit character.
 * @return {?string} Chopped character if there is preedit string,
 *     otherwise null.
 */
skk.mode.Preedit.prototype.backspace = function() {
  console.debug('skk.mode.Preedit.backspace');
  if (this.composer_.hasUndetermined()) {
    return this.composer_.backspace();
  }

  if (!this.hasPreedit()) { return null; }

  var chopped = this.preedit_.substr(-1);
  this.preedit_ = this.preedit_.slice(0, -1);
  return chopped;
};

/**
 * Looks up conversion candidates.
 * @param {string} base No conjugation part of word. 1+ length Hiragana string.
 * @param {string} stem Stem of conjugation. 0+ length latin string.
 * @param {string} conjugated First character of conjugated suffix kana.
 * @param {boolean=} opt_predictive If true was set, lists predicted candidates
 *     instead of exactly matching ones.
 */
skk.mode.Preedit.prototype.lookupCandidate = function(base,
                                                      stem,
                                                      conjugated,
                                                      opt_predictive) {
  var self = this;
  var conjugations = skk.VOWELS.map(
      function(vowel) {
        var roman = stem + vowel;
        return skk.Composer.map.hiragana[roman];
      }).filter(function(kana) { return !!kana; });
  var conjugationPattern = new RegExp('(' + conjugations.join('|') + ')$');

  self.ime_.dict.lookup(
      base, stem,
      function(isError, candidates, predictions) {
        if (isError) {
          console.error('skk.mode.Preedit.lookupCandidate: Error occured');
          return;
        }

        // TODO(sekia): Implement predictive conversion
        self.ime_.enterNewMode(skk.InputMode.CONVERSION, function(mode) {
          if (stem !== '') {
            candidates = Array.uniq(candidates.filter(
                function(candidate) {
                  return !!candidate.match(conjugationPattern);
                }).map(
                    function(candidate) {
                      return candidate.slice(0, -1) + conjugated;
                    }));
          }
          if (candidates.length === 0) { candidates = [base + conjugated]; }
          mode.setWordBaseAndStem(base, stem);
          mode.setPreedit(base + conjugated);
          mode.setCandidates(candidates);
        });
      });
};

/**
 * @inheritDoc
 */
skk.mode.Preedit.prototype.prepareForKey = function(keyEvent) {
  if (keyEvent.key === 'j' && keyEvent.ctrlKey) {
    this.ime_.leaveCurrentMode();
    return null;
  }

  if (skk.util.isArrow(keyEvent.key)) { return null; }

  if (skk.util.isSpace(keyEvent.key) &&
      this.hasPreedit() && !this.composer_.hasUndetermined()) {
    this.lookupCandidate(this.preedit_, '', '');
    return null;
  }

  if (skk.util.isBackspace(keyEvent.key)) {
    var chopped = this.backspace();
    console.debug('Chopped: ', chopped);
    if (this.composer_.undetermined === '') {
      this.waitingDeterminant_ = false;
    }

    if (!!chopped) {
      skk.util.showPreedit(
          this.ime_.context.contextID,
          this.mark_ + this.preedit_ + this.composer_.undetermined);
    } else {  // There's no preedit text any more
      this.ime_.leaveCurrentMode();
    }
    return null;
  }

  if (skk.util.isEnter(keyEvent.key)) {
    chrome.experimental.input.commitText(
        {
          contextID: this.ime_.context.contextID,
          text: this.preedit_ + this.composer_.undetermined
        });
    this.preedit_ = '';
    this.composer_.undetermined = '';
    skk.util.showPreedit(this.ime_.context.contextID, this.preedit_);
    return null;
  }

  return keyEvent;
};

/**
 * @inheritDoc
 */
skk.mode.Preedit.prototype.addKey = function(keyEvent) {
  console.debug('skk.mode.Preedit.addKey: ', keyEvent.key);

  if (!skk.util.isPrintable(keyEvent.key)) { return false; }

  if (skk.util.isUpperLetter(keyEvent.key) &&
      !skk.util.isVowel(keyEvent.key) && this.hasPreedit()) {
    this.waitingDeterminant_ = true;
  }

  keyEvent.key = keyEvent.key.toLowerCase();

  console.debug('skk.mode.Preedit.addKey: ',
                'Undetermined: ', this.composer_.undetermined);

  var base = this.preedit_;
  var stem = this.composer_.undetermined;
  var determAndUndeterm = this.composer_.addKey(keyEvent.key);
  var determined = determAndUndeterm[0] || '';
  var undetermined = determAndUndeterm[1] || '';

  if (this.waitingDeterminant_ && determined !== '') {
    this.lookupCandidate(base, stem, determined);
    return true;
  }

  this.preedit_ += determined;
  skk.util.showPreedit(
      this.ime_.context.contextID,
      this.mark_ + this.preedit_ +
          (this.waitingDeterminant_ ? '*' : '') + undetermined);

  return true;
};

/**
 * Initializes candidate select mode.
 * @constructor
 * @implements {skk.mode.InputModeInterface}
 * @param {skk.IME} ime IME instance.
 */
skk.mode.Conversion = function(ime) {
  this.mark_ = '\u25bc';  // '▼'; default selection mark
  this.ime_ = ime;
  this.base_ = null;
  this.stem_ = null;
  this.preedit_ = null;
  this.candidates_ = null;
  this.selectedIndex_ = 0;
};

/**
 * @param {string} base No conjugation part of word. 1+ length Hiragana string.
 * @param {string} stem Stem of conjugation. 0+ length latin string.
 */
skk.mode.Conversion.prototype.setWordBaseAndStem = function(base, stem) {
  this.base_ = base;
  this.stem_ = stem;
};

/**
 * Sets symbol character that will appear in head of preedit.
 * @param {string} mark Symbol character.
 */
skk.mode.Conversion.prototype.setPreeditMark = function(mark) {
  this.mark_ = mark;
};

/**
 * Sets preedit string to be shown.
 * @param {string} preedit Preedit text.
 */
skk.mode.Conversion.prototype.setPreedit = function(preedit) {
  this.preedit_ = preedit;
};

/**
 * Sets conversion candidates to be displayed.
 * @param {Array.<string>} candidates Candidates to be listed.
 */
skk.mode.Conversion.prototype.setCandidates = function(candidates) {
  this.candidates_ = candidates;
  chrome.experimental.input.setCandidates(
      {
        contextID: this.ime_.context.contextID,
        candidates: this.candidates_.map(
            function(candidate, index) {
              return {
                candidate: candidate,
                id: index
              };
            })
      });
};

/**
 * @inheritDoc
 */
skk.mode.Conversion.prototype.reset = function() {
  console.debug('skk.mode.Conversion.reset');
  this.ime_.leaveCurrentMode();  // Conversion mode

  console.assert(this.ime_.getCurrentMode() instanceof skk.mode.Preedit,
                 'skk.mode.Conversion:reset: Internal error: ' +
                     'Current input mode must be skk.mode.Preedit');
  this.ime_.leaveCurrentMode();  // Preedit mode
};

/**
 * @inheritDoc
 */
skk.mode.Conversion.prototype.enter = function() {
  console.debug('skk.mode.Conversion.enter');
  skk.util.showPreedit(
      this.ime_.context.contextID, this.mark_ + this.preedit_);
  this.showCandidateWindow();
};

/**
 * @inheritDoc
 */
skk.mode.Conversion.prototype.leave = function() {
  console.debug('skk.mode.Conversion.leave');
  this.preedit_ = '';
  this.candidates_ = [];
  skk.util.showPreedit(this.ime_.context.contextID, this.preedit_);
  this.hideCandidateWindow();
};

/**
 * @inheritDoc
 */
skk.mode.Conversion.prototype.resume = function() {
  console.debug('skk.mode.Conversion.resume');
  skk.util.showPreedit(
      this.ime_.context.contextID, this.mark_ + this.preedit_);
  this.showCandidateWindow();
};

/**
 * @inheritDoc
 */
skk.mode.Conversion.prototype.suspend = function() {
  console.debug('skk.mode.Conversion.suspend');
  skk.util.showPreedit(this.ime_.context.contextID, '');
  this.hideCandidateWindow();
};

/**
 * Commits selected candidates and leaves kanji mode.
 * @param {integer} candidateId 0-origin candidate index.
 */
skk.mode.Conversion.prototype.commitCandidate = function(candidateId) {
  var selection = this.candidates_[candidateId];
  this.ime_.dict.addHistory(this.base_, this.stem_, selection);
  chrome.experimental.input.commitText(
      {
        contextID: this.ime_.context.contextID,
        text: selection
      });
  skk.util.showPreedit(this.ime_.context.contextID, '');
  this.ime_.leaveCurrentMode();  // Leave from Conversion mode
  console.assert(this.ime_.getCurrentMode() instanceof skk.mode.Preedit,
                 'skk.mode.Conversion:commitCandidate: Internal error: ' +
                     'Current input mode must be skk.mode.Preedit');
  this.ime_.leaveCurrentMode();  // Leave from Preedit mode
};

/**
 * @inheritDoc
 */
skk.mode.Conversion.prototype.prepareForKey = function(keyEvent) {
  if ((keyEvent.key == 'g' && keyEvent.ctrlKey) ||
      skk.util.isBackspace(keyEvent.key) || skk.util.isEscape(keyEvent.key)) {
    // cancel
    this.ime_.leaveCurrentMode();
    return null;
  }

  if (skk.util.isSpace(keyEvent.key)) {
    this.selectedIndex_ += skk.NUM_CANDIDATES;
    // TODO(sekia): implement word registering
    this.selectedIndex_ %= this.candidates_.length;
    chrome.experimental.input.setCursorPosition(
        {
          contextID: this.ime_.context.contextID,
          candidateID: this.selectedIndex_
        });
    return null;
  }

  if (keyEvent.key == 'x') {
    // previous page.
    this.selectedIndex_ -= skk.NUM_CANDIDATES;
    // TODO(sekia): implement word registering
    if (this.selectedIndex_ < 0) {
      // cancel to the previous mode.
      this.ime_.leaveCurrentMode();
      return null;
    }

    chrome.experimental.input.setCursorPosition(
        {
          contextID: this.ime_.context.contextID,
          candidateID: this.selectedIndex_
        });
    return null;
  }

  // If the key is nothing special, it just commits the candidate and
  // handle as usual.  SKK does not have focus on the candidate window
  // and always commit the first one.
  var index = this.selectedIndex_ - (this.selectedIndex_ % skk.NUM_CANDIDATES);
  var isIndexKey = false;

  var indexMap = { a: 0, s: 1, d: 2, f: 3, j: 4, k: 5, l: 6 };
  if (keyEvent.key in indexMap) {
    // Do not handle the key as usual if the key is selecting a candidate.
    index += indexMap[keyEvent.key];
    isIndexKey = true;
  }

  this.commitCandidate(index);
  if (isIndexKey) {
    return null;
  } else {
    return keyEvent;
  }
};

/**
 * @inheritDoc
 */
skk.mode.Conversion.prototype.addKey = function(keyEvent) {
  console.error('skk.mode.Conversion.addKey: Must not reach here');
  return false;
};

/**
 * Displays candidate window.
 */
skk.mode.Conversion.prototype.showCandidateWindow = function() {
  chrome.experimental.input.setCandidateWindowProperties(
      {
        engineID: this.ime_.engineID,
        properties: { visible: true }
      });
};

/**
 * Hides candidate window.
 */
skk.mode.Conversion.prototype.hideCandidateWindow = function() {
  chrome.experimental.input.setCandidateWindowProperties(
      {
        engineID: this.ime_.engineID,
        properties: { visible: false }
      });
};