<commit_before>import { isDesktopApplication } from '@/utils';
import pull from 'lodash/pull';
import {
  ProtectedAction,
  ApplicationEvent,
  SNTag,
  SNNote,
  SNUserPrefs,
  ContentType,
  SNSmartTag,
  PayloadSource
} from 'snjs';
import { WebApplication } from '@/ui_models/application';
import { Editor } from '@/ui_models/editor';

export enum AppStateEvent {
  TagChanged = 1,
  ActiveEditorChanged = 2,
  PreferencesChanged = 3,
  PanelResized = 4,
  EditorFocused = 5,
  BeganBackupDownload = 6,
  EndedBackupDownload = 7,
  DesktopExtsReady = 8,
  WindowDidFocus = 9,
  WindowDidBlur = 10,
};

export enum EventSource {
  UserInteraction = 1,
  Script = 2
};

type ObserverCallback = (event: AppStateEvent, data?: any) => Promise<void>

export class AppState {
  $rootScope: ng.IRootScopeService
  $timeout: ng.ITimeoutService
  application: WebApplication
  observers: ObserverCallback[] = []
  locked = true
  unsubApp: any
  rootScopeCleanup1: any
  rootScopeCleanup2: any
  onVisibilityChange: any
  selectedTag?: SNTag
  userPreferences?: SNUserPrefs
  multiEditorEnabled = false

  /* @ngInject */
  constructor(
    $rootScope: ng.IRootScopeService,
    $timeout: ng.ITimeoutService,
    application: WebApplication
  ) {
    this.$timeout = $timeout;
    this.$rootScope = $rootScope;
    this.application = application;
    this.registerVisibilityObservers();
    this.addAppEventObserver();
    this.streamNotesAndTags();
    const onVisibilityChange = () => {
      const visible = document.visibilityState === "visible";
      const event = visible
        ? AppStateEvent.WindowDidFocus
        : AppStateEvent.WindowDidBlur;
      this.notifyEvent(event);
    }
    this.onVisibilityChange = onVisibilityChange.bind(this);
  }

  deinit() {
    this.unsubApp();
    this.unsubApp = undefined;
    this.observers.length = 0;
    if (this.rootScopeCleanup1) {
      this.rootScopeCleanup1();
      this.rootScopeCleanup2();
      this.rootScopeCleanup1 = undefined;
      this.rootScopeCleanup2 = undefined;
    }
    document.removeEventListener('visibilitychange', this.onVisibilityChange);
    this.onVisibilityChange = undefined;
  }

  /**
   * Creates a new editor if one doesn't exist. If one does, we'll replace the
   * editor's note with an empty one.
   */
  async createEditor(title?: string) {
    const activeEditor = this.getActiveEditor();
    if (!activeEditor || this.multiEditorEnabled) {
      this.application.editorGroup.createEditor(undefined, title);
    } else {
      await activeEditor.reset(title);
    }
  }

  async openEditor(noteUuid: string) {
    const note = this.application.findItem(noteUuid) as SNNote;
    const run = async () => {
      const activeEditor = this.getActiveEditor();
      if (!activeEditor || this.multiEditorEnabled) {
        this.application.editorGroup.createEditor(noteUuid);
      } else {<commit_msg>fix: keep state consistent when clicking placeholder note<commit_after>import { isDesktopApplication } from '@/utils';
import pull from 'lodash/pull';
import {
  ProtectedAction,
  ApplicationEvent,
  SNTag,
  SNNote,
  SNUserPrefs,
  ContentType,
  SNSmartTag,
  PayloadSource
} from 'snjs';
import { WebApplication } from '@/ui_models/application';
import { Editor } from '@/ui_models/editor';

export enum AppStateEvent {
  TagChanged = 1,
  ActiveEditorChanged = 2,
  PreferencesChanged = 3,
  PanelResized = 4,
  EditorFocused = 5,
  BeganBackupDownload = 6,
  EndedBackupDownload = 7,
  DesktopExtsReady = 8,
  WindowDidFocus = 9,
  WindowDidBlur = 10,
};

export enum EventSource {
  UserInteraction = 1,
  Script = 2
};

type ObserverCallback = (event: AppStateEvent, data?: any) => Promise<void>

export class AppState {
  $rootScope: ng.IRootScopeService
  $timeout: ng.ITimeoutService
  application: WebApplication
  observers: ObserverCallback[] = []
  locked = true
  unsubApp: any
  rootScopeCleanup1: any
  rootScopeCleanup2: any
  onVisibilityChange: any
  selectedTag?: SNTag
  userPreferences?: SNUserPrefs
  multiEditorEnabled = false

  /* @ngInject */
  constructor(
    $rootScope: ng.IRootScopeService,
    $timeout: ng.ITimeoutService,
    application: WebApplication
  ) {
    this.$timeout = $timeout;
    this.$rootScope = $rootScope;
    this.application = application;
    this.registerVisibilityObservers();
    this.addAppEventObserver();
    this.streamNotesAndTags();
    const onVisibilityChange = () => {
      const visible = document.visibilityState === "visible";
      const event = visible
        ? AppStateEvent.WindowDidFocus
        : AppStateEvent.WindowDidBlur;
      this.notifyEvent(event);
    }
    this.onVisibilityChange = onVisibilityChange.bind(this);
  }

  deinit() {
    this.unsubApp();
    this.unsubApp = undefined;
    this.observers.length = 0;
    if (this.rootScopeCleanup1) {
      this.rootScopeCleanup1();
      this.rootScopeCleanup2();
      this.rootScopeCleanup1 = undefined;
      this.rootScopeCleanup2 = undefined;
    }
    document.removeEventListener('visibilitychange', this.onVisibilityChange);
    this.onVisibilityChange = undefined;
  }

  /**
   * Creates a new editor if one doesn't exist. If one does, we'll replace the
   * editor's note with an empty one.
   */
  async createEditor(title?: string) {
    const activeEditor = this.getActiveEditor();
    if (!activeEditor || this.multiEditorEnabled) {
      this.application.editorGroup.createEditor(undefined, title);
    } else {
      await activeEditor.reset(title);
    }
  }

  async openEditor(noteUuid: string) {
    const note = this.application.findItem(noteUuid) as SNNote;
    if (this.getActiveEditor()?.note?.uuid === noteUuid) return;
    const run = async () => {
      const activeEditor = this.getActiveEditor();
      if (!activeEditor || this.multiEditorEnabled) {
        this.application.editorGroup.createEditor(noteUuid);
      } else {
{'fraction_non_alphanumeric': 0.10637213254035684, 'fraction_numerical': 0.00713678844519966, 'mean_word_length': 3.641955835962145, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3739218', 'n_tokens_mistral': 1820, 'n_tokens_neox': 1771, 'n_words': 397}