<commit_before>Mod.require 'Weya.Base',
 'Weya'
 'Editor'
 'Help'
 (Base, Weya, Editor, Help) ->
  ELECTRON = require 'electron'
  IPC = ELECTRON.ipcRenderer
  FS = require 'fs'
  PATH = require 'path'

  TEMPORARY_FILE = 'temporary.swp.ds'
  OPTIONS_FILE = 'options.json'
  TEMPORARY_SAVE_INTERVAL = 10 * 1000
  CHANGED_WATCH_INTERVAL = 500

  PROTOCOLS = [
   'https://'
   'http://'
   'file://'
  ]

  window.wallapattaDecodeURL = (url) ->
   for protocol in PROTOCOLS
    if (url.substr 0, protocol.length) is protocol
     return url

   return url if not APP.folder?
   url = "/#{url}" if url[0] isnt '/'
   return "#{APP.folder.url}#{url}"

  class App extends Base
   @initialize ->
    @elems = {}
    @resources = {}
    @_editorChanged = false
    @content = ''
    @options =
     file: ''
     folder: ''
    @editor = new Editor
     openUrl: @on.openUrl
     onChanged: @on.editorChanged
     app: this

   load: (callback) ->
    IPC.on 'userDataPath', (e, path) =>
     @_userDataPath = path
     try
      options = "#{FS.readFileSync PATH.join @_userDataPath, OPTIONS_FILE}"
      @options = JSON.parse options
     catch e
      @options =
       file: ''
       folder: ''
     @setFolder()
     @setFile()
     if @file?
      try
       @content = "#{FS.readFileSync @file.path}"
      catch e
       @content = '=====Error reading file====='
     else
      @content = null

     try
      @_tempContent = "#{FS.readFileSync PATH.join @_userDataPath, TEMPORARY_FILE}"
     catch e
      if @content?
       @_tempContent = @content
      else
       @_tempContent = @content = Help
     callback()
    IPC.send 'getUserDataPath'

   @listen 'error', (e) ->
    console.error e

   @listen 'editorChanged', ->
    @_editorChanged = true

   @listen 'openUrl', (url) ->
    @send 'openUrl', url: url


   render: (callback) ->
    @elems.container = document.body
    Weya elem: @elems.container, context: this, ->
     @div ".window", ->
      @header ".toolbar.toolbar-header", ->
       @div ".toolbar-actions", ->
        @div ".btn-group", ->
         @button ".btn.btn-default",
          title: "Select images folder"
          on: {click: @$.on.folder}
          ->
           @span ".icon.icon-folder", null
         @button ".btn.btn-default",
          title: "Open file"
          on: {click: @$.on.file}
          ->
           @span ".icon.icon-upload", null
         @$.elems.save = @button ".btn.btn-default",
          title: "Save file"
          style: {display: 'none'}
          on: {click: @$.on.save}
          ->
           @span ".icon.icon-download.icon-text", null
           @$.elems.saveName = @span ""
         @button ".btn.btn-default",
          title: "Save file"
          on: {click: @$.on.saveAs}
          "SaveAs"
         @$.elems.printBtn = @button ".btn.btn-default",
           title: "Print"
           on: {click: @$.on.print}
           ->
            @span ".icon.icon-print", null
         @$.elems.editBtn = @button ".btn.btn-positive",
           title: "Edit"
           on: {click: @$.on.edit}
           style: {display: 'none'}
           ->
            @span ".icon.icon-pencil", null

        @$.elems.editorToolbar = @span ""

        @button ".btn.btn-default.pull-right",
         title: "Help"
         on: {click: @$.on.help}
         ->
          @span ".icon.icon-help", null

      @div ".window-content", ->
       @$.elems.editor = @div ".editor", ''

    @editor.render @elems.editor, @elems.editorToolbar, =>
     @_watchInterval = setInterval @on.watchChanges, CHANGED_WATCH_INTERVAL
     @_saveTemInterval = setInterval @on.saveTemporary, TEMPORARY_SAVE_INTERVAL
     @editor.setText @_tempContent
     delete @_tempContent
     if @file?
      @elems.save.style.display = 'inline-block'
     @on.watchChanges()
     callback()

   @listen 'folder', -> IPC.send 'openFolder'
   @listen 'folderOpened', (e, folders) ->
    console.log folders
    return if not folders?
    return if folders.length <= 0
    @options.folder = folders[0]
    @setFolder()
    @saveOptions()
    @editor.setText @removeTrailingSpace @editor.getText()

   @listen 'file', -> IPC.send 'openFile'
   @listen 'fileOpened', (e, files) ->
    return if not files?
    return if files.length <= 0
    @options.file = files[0]
    @setFile()

    @content = "#{FS.readFileSync @file.path}"
    @editor.setText @content
    @_editorChanged = true
    @on.saveTemporary()
    @saveOptions()
    @elems.save.style.display = 'inline-block'
    @elems.saveName.textContent = "#{@file.name}"

   @listen 'save', ->
    @content = @removeTrailingSpace @editor.getText()
    @editor.setText @content
    FS.writeFile @file.path, @content, (err) ->
     if err?
      console.error err
     else
      console.log 'file saved'

   @listen 'saveAs', -> IPC.send 'saveFile'
   @listen 'saveFile', (e, file) ->
    return if not file?
    @options.file = file
    @setFile()

    @content = @removeTrailingSpace @editor.getText()
    @editor.setText @content
    @_editorChanged = true
    @on.saveTemporary()
    @saveOptions()
    @elems.save.style.display = 'inline-block'
    @elems.saveName.textContent = "#{@file.name}"
    FS.writeFile @file.path, @content, (err) ->
     if err?
      console.error err
     else
      console.log 'file saved'


   @listen 'print', ->
    @elems.printBtn.style.display = 'none'
    @elems.editBtn.style.display = 'inline-block'
    @editor.print()<commit_msg>pick media list<commit_after>Mod.require 'Weya.Base',
 'Weya'
 'Editor'
 'Help'
 (Base, Weya, Editor, Help) ->
  ELECTRON = require 'electron'
  IPC = ELECTRON.ipcRenderer
  FS = require 'fs'
  PATH = require 'path'

  TEMPORARY_FILE = 'temporary.swp.ds'
  OPTIONS_FILE = 'options.json'
  TEMPORARY_SAVE_INTERVAL = 10 * 1000
  CHANGED_WATCH_INTERVAL = 500

  PROTOCOLS = [
   'https://'
   'http://'
   'file://'
  ]

  window.wallapattaDecodeURL = (url) ->
   for protocol in PROTOCOLS
    if (url.substr 0, protocol.length) is protocol
     return url

   return url if not APP.folder?
   url = "/#{url}" if url[0] isnt '/'
   return "#{APP.folder.url}#{url}"

  class App extends Base
   @initialize ->
    @elems = {}
    @resources = {}
    @_editorChanged = false
    @content = ''
    @options =
     file: ''
     folder: ''
    @editor = new Editor
     openUrl: @on.openUrl
     onChanged: @on.editorChanged
     app: this

   load: (callback) ->
    IPC.on 'userDataPath', (e, path) =>
     @_userDataPath = path
     try
      options = "#{FS.readFileSync PATH.join @_userDataPath, OPTIONS_FILE}"
      @options = JSON.parse options
     catch e
      @options =
       file: ''
       folder: ''
     @setFolder()
     @setFile()
     if @file?
      try
       @content = "#{FS.readFileSync @file.path}"
      catch e
       @content = '=====Error reading file====='
     else
      @content = null

     try
      @_tempContent = "#{FS.readFileSync PATH.join @_userDataPath, TEMPORARY_FILE}"
     catch e
      if @content?
       @_tempContent = @content
      else
       @_tempContent = @content = Help
     callback()
    IPC.send 'getUserDataPath'

   @listen 'error', (e) ->
    console.error e

   @listen 'editorChanged', ->
    @_editorChanged = true

   @listen 'openUrl', (url) ->
    @send 'openUrl', url: url


   render: (callback) ->
    @elems.container = document.body
    Weya elem: @elems.container, context: this, ->
     btn = (icon, event, title) ->
      @button ".btn.btn-default",
       title: title
       on: {click: @$.on[event]}
       ->
        @i ".fa.fa-#{icon}", null


     @div ".window", ->
      @header ".toolbar.toolbar-header", ->
       @div ".toolbar-actions", ->
        @div ".btn-group", ->
         btn.call this, 'folder', 'folder', 'Select images folder'
         btn.call this, 'upload', 'file', 'Open file'
         #@$.elems.save = btn.call this, 'download', 'save', 'Save file'
         @$.elems.save = @button ".btn.btn-default",
          title: "Save file"
          style: {display: 'none'}
          on: {click: @$.on.save}
          ->
           @i ".fa.fa-download.icon-text", null
           #@span ".icon.icon-download.icon-text", null
           @$.elems.saveName = @span ""

         @button ".btn.btn-default",
          title: "Save file"
          on: {click: @$.on.saveAs}
          "Save As"

         @$.elems.print = btn.call this, 'print', 'print', 'Print'
         @$.elems.printBtn = @button ".btn.btn-default",
           title: "Print"
           on: {click: @$.on.print}
           ->
            @span ".icon.icon-print", null
         @$.elems.editBtn = @button ".btn.btn-positive",
           title: "Edit"
           on: {click: @$.on.edit}
           style: {display: 'none'}
           ->
            @i ".fa.fa-pencil", null

        @$.elems.editorToolbar = @span ""

        @button ".btn.btn-default.pull-right",
         title: "Help"
         on: {click: @$.on.help}
         ->
          @span ".icon.icon-help", null

      @div ".window-content", ->
       @$.elems.editor = @div ".editor", ''

    @editor.render @elems.editor, @elems.editorToolbar, =>
     @_watchInterval = setInterval @on.watchChanges, CHANGED_WATCH_INTERVAL
     @_saveTemInterval = setInterval @on.saveTemporary, TEMPORARY_SAVE_INTERVAL
     @editor.setText @_tempContent
     delete @_tempContent
     if @file?
      @elems.saveBtn.style.display = 'inline-block'
     @on.watchChanges()
     callback()

   @listen 'folder', -> IPC.send 'openFolder'
   @listen 'folderOpened', (e, folders) ->
    console.log folders
    return if not folders?
    return if folders.length <= 0
    @options.folder = folders[0]
    @setFolder()
    @saveOptions()
    @editor.setText @removeTrailingSpace @editor.getText()

   @listen 'file', -> IPC.send 'openFile'
   @listen 'fileOpened', (e, files) ->
    return if not files?
    return if files.length <= 0
    @options.file = files[0]
    @setFile()

    @content = "#{FS.readFileSync @file.path}"
    @editor.setText @content
    @_editorChanged = true
    @on.saveTemporary()
    @saveOptions()
    @elems.saveBtn.style.display = 'inline-block'
    @elems.saveName.textContent = "#{@file.name}"

   @listen 'save', ->
    @content = @removeTrailingSpace @editor.getText()
    @editor.setText @content
    FS.writeFile @file.path, @content, (err) ->
     if err?
      console.error err
     else
      console.log 'file saved'

   @listen 'saveAs', -> IPC.send 'saveFile'
   @listen 'saveFile', (e, file) ->
    return if not file?
    @options.file = file
    @setFile()

    @content = @removeTrailingSpace @editor.getText()
    @editor.setText @content
    @_editorChanged = true
    @on.saveTemporary()
    @saveOptions()
    @elems.saveBtn.style.display = 'inline-block'
    @elems.saveName.textContent = "#{@file.name}"
    FS.writeFile @file.path, @content, (err) ->
     if err?
      console.error err
     else
      console.log 'file saved'


   @listen 'print', ->
    @elems.printBtn.style.display = 'none'
    @elems.editBtn.style.display = 'inline-block'
    @editor.print()
{'fraction_non_alphanumeric': 0.15585471629924105, 'fraction_numerical': 0.0027105168052041924, 'mean_word_length': 2.215862870424172, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 67, 'https://': 2, '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': '6113991', 'n_tokens_mistral': 3758, 'n_tokens_neox': 3640, 'n_words': 836}