aboutsummaryrefslogtreecommitdiff
path: root/src/content/presenters/FindPresenter.ts
blob: d9bc8352ad363b14b2032ac6740017d81d85af40 (plain) (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
export default interface FindPresenter {
  find(keyword: string, backwards: boolean): boolean;

  clearSelection(): void;

  // eslint-disable-next-line semi
}

// window.find(aString, aCaseSensitive, aBackwards, aWrapAround,
//             aWholeWord, aSearchInFrames);
//
// NOTE: window.find is not standard API
// https://developer.mozilla.org/en-US/docs/Web/API/Window/find
interface MyWindow extends Window {
  find(
    aString: string,
    aCaseSensitive?: boolean,
    aBackwards?: boolean,
    aWrapAround?: boolean,
    aWholeWord?: boolean,
    aSearchInFrames?: boolean,
    aShowDialog?: boolean): boolean;
}

// eslint-disable-next-line no-var, vars-on-top, init-declarations
declare var window: MyWindow;

export class FindPresenterImpl implements FindPresenter {
  find(keyword: string, backwards: boolean): boolean {
    let caseSensitive = false;
    let wrapScan = true;


    // NOTE: aWholeWord dows not implemented, and aSearchInFrames does not work
    // because of same origin policy
    let found = window.find(keyword, caseSensitive, backwards, wrapScan);
    if (found) {
      return found;
    }
    this.clearSelection();

    return window.find(keyword, caseSensitive, backwards, wrapScan);
  }

  clearSelection(): void {
    let sel = window.getSelection();
    if (sel) {
      sel.removeAllRanges();
    }
  }
}