use fat arrow for this reference

This commit is contained in:
Xuan Sang LE
2020-05-17 17:05:12 +02:00
parent 95cfddb389
commit fa22fc5413
41 changed files with 1086 additions and 1072 deletions

View File

@ -3,27 +3,26 @@ class CommandPalette extends this.OS.GUI.BasicDialog
super "CommandPalete", CommandPalette.scheme
init: () ->
me = @
offset = $(".afx-window-content", @parent.scheme).offset()
pw = @parent.scheme.get("width") / 5
@scheme.set "width", 3 * pw
$(@scheme).offset { top: offset.top - 2, left: offset.left + pw }
cb = (e) ->
if ($ e.target).closest(me.scheme).length > 0
$(me.find "searchbox").focus()
cb = (e) =>
if ($ e.target).closest(@scheme).length > 0
$(@find "searchbox").focus()
else
$(document).unbind "mousedown", cb
me.quit()
@quit()
$(document).on "mousedown", cb
$(me.find "searchbox").focus()
$(@find "searchbox").focus()
@cmdlist = @find("container")
@cmdlist.set "data", (v for v in @data.child) if @data
$(@cmdlist).click (e) ->
me.selectCommand()
$(@cmdlist).click (e) =>
@selectCommand()
@searchbox = @find "searchbox"
($ @searchbox).keyup (e) ->
me.search e
($ @searchbox).keyup (e) =>
@search e
search: (e) ->
switch e.which

View File

@ -8,42 +8,45 @@ class App.extensions.AntOSDK extends App.BaseExtension
# public functions
create: () ->
me = @
@app.openDialog("FileDialog", {
title: "__(New Project at)",
file: { basename: __("ProjectName") },
mimes: ["dir"]
}).then (d) ->
me.mktpl d.file.path, d.name, true
}).then (d) =>
@mktpl d.file.path, d.name, true
init: () ->
me = @
dir = @app.currdir
return @create() unless dir and dir.basename
dir.read()
.then (d) ->
return me.notify __("Cannot read folder: {0}", dir.path) if d.error
return me.notify __("The folder is not empty: {0}", dir.path) unless d.result.length is 0
me.mktpl dir.parent().path, dir.basename
.then (d) =>
return @notify __("Cannot read folder: {0}", dir.path) if d.error
return @notify __("The folder is not empty: {0}", dir.path) unless d.result.length is 0
@mktpl dir.parent().path, dir.basename
build: () ->
console.log "build"
release: () ->
console.log "release"
options: () ->
console.log "options"
buildnrun: () ->
@metadata().then (meta) =>
@build(meta).then () =>
@run(meta).catch (e) => @error.toString()
.catch (e) =>
@error e.toString()
.catch (e) => @error e.toString()
release: () ->
@metadata().then (meta) =>
@build(meta).then () =>
@mkar(meta)
.then () ->
.catch (e) => @error.toString()
.catch (e) =>
@error e.toString()
.catch (e) => @error e.toString()
dependencies: () ->
[
"AntOSDK/coffeescript.js"
]
# private functions
mktpl: (path, name, flag) ->
me = @
rpath = "#{path}/#{name}"
console.log rpath
dirs = [
"#{rpath}/build",
"#{rpath}/build/release",
@ -57,49 +60,198 @@ class App.extensions.AntOSDK extends App.BaseExtension
files = [
["main.tpl", "#{rpath}/coffees/main.coffee"],
["package.tpl", "#{rpath}/package.json"],
["project.tpl", "#{rpath}/project.apj"],
["project.tpl", "#{rpath}/project.json"],
["README.tpl", "#{rpath}/README.md"],
["scheme.tpl", "#{rpath}/assets/scheme.html"]
]
@mkdirAll dirs
.then () ->
me.mkfileAll(files, path, name)
.then () ->
me.app.currdir = rpath.asFileHandle()
me.app.initSideBar()
me.app.openFile "#{rpath}/README.md".asFileHandle()
.catch (e) -> me.error e.stack
.catch (e) -> me.error e.stack
.then () =>
@mkfileAll(files, path, name)
.then () =>
@app.currdir = rpath.asFileHandle()
@app.initSideBar()
@app.openFile "#{rpath}/README.md".asFileHandle()
.catch (e) => @error e.stack
.catch (e) => @error e.stack
mkdirAll: (list) ->
me = @
new Promise (resolve, reject) ->
new Promise (resolve, reject) =>
return resolve() if list.length is 0
path = (list.splice 0, 1)[0].asFileHandle()
console.log path.parent().path, path.basename
path.parent().mk path.basename
.then () ->
me.mkdirAll list
.then (d) =>
@mkdirAll list
.then () -> resolve()
.catch (e) -> reject e
.catch (e) -> reject e
mkfileAll: (list, path, name) ->
me = @
new Promise (resolve, reject) ->
new Promise (resolve, reject) =>
return resolve() if list.length is 0
item = (list.splice 0, 1)[0]
"#{me.basedir()}/AntOSDK/templates/#{item[0]}"
"#{@basedir()}/AntOSDK/templates/#{item[0]}"
.asFileHandle()
.read()
.then (data) ->
.then (data) =>
file = item[1].asFileHandle()
.setCache(data.format name, "#{path}/#{name}")
.write "text/plain"
.then () ->
me.mkfileAll list, path, name
.then () =>
@mkfileAll list, path, name
.then () -> resolve()
.catch (e) -> reject e
.catch (e) -> reject e
.catch (e) -> reject e
metadata: () ->
new Promise (resolve, reject) =>
if not @app.currdir
return reject @app._api.throwe __("Project folder is not found")
"#{@app.currdir.path}/project.json"
.asFileHandle()
.read("json")
.then (data) ->
resolve data
.catch (e) =>
reject @app._api.throwe __("Unable to read project meta-data")
verify: (list) ->
new Promise (resolve, reject) =>
return resolve() if list.length is 0
file = (list.splice 0, 1)[0].asFileHandle()
@notify __("Verifying: {0}", file.path)
file.read().then (data) =>
try
CoffeeScript.nodes data
@verify list
.then () -> resolve()
.catch (e) -> reject e
catch ex
reject ex
.catch (e) -> reject e
compile: (meta) ->
new Promise (resolve, reject) =>
@import("#{@basedir()}/AntOSDK/coffeescript.js").then () =>
list = ("#{meta.root}/#{v}" for v in meta.coffees)
@verify((f for f in list)).then () =>
@cat(list).then (code) =>
jsrc = CoffeeScript.compile code
@notify __("Compiled successful")
resolve jsrc
.catch (e) -> reject e
.catch (e) -> reject e
.catch (e) -> reject e
build: (meta) ->
new Promise (resolve, reject) =>
@compile(meta).then (src) =>
@cat ("#{meta.root}/#{v}" for v in meta.javascripts), src
.then (jsrc) ->
new Promise (r, e) ->
"#{meta.root}/build/debug/main.js"
.asFileHandle()
.setCache jsrc
.write("text/plain")
.then (d) ->
return e d if d.error
r()
.catch (ex) -> e ex
.then () =>
new Promise (r, e) =>
@cat ("#{meta.root}/#{v}" for v in meta.css), ""
.then (txt) ->
return r() if txt is ""
"#{meta.root}/build/debug/main.css"
.asFileHandle()
.setCache txt
.write("text/plain")
.then (d) ->
return e d if d.error
r()
.catch (ex) -> e ex
.then () =>
@copy ("#{meta.root}/#{v}" for v in meta.copies), "#{meta.root}/build/debug"
.then () -> resolve()
.catch (e) -> reject e
.catch (e) -> reject e
run: (meta) ->
"#{meta.root}/build/debug/package.json"
.asFileHandle()
.read("json")
.then (v) =>
v.text = v.name
v.path = "#{meta.root}/build/debug"
v.filename = meta.name
v.type = "app"
v.mime = "antos/app"
v.icon = "#{v.path}/#{v.icon}" if v.icon
v.iconclass = "fa fa-adn" unless v.iconclass or v.icon
@notify __("Installing...")
@app.systemsetting.system.packages[meta.name] = v
@notify __("Running {0}...", meta.name)
@app._gui.forceLaunch meta.name
cat: (list, data) ->
new Promise (resolve, reject) =>
return resolve data if list.length is 0
file = (list.splice 0, 1)[0].asFileHandle()
file
.read()
.then (text) =>
data = data + "\n" + text
@cat list, data
.then (d) -> resolve d
.catch (e) -> reject e
.catch (e) -> reject e
copy: (files, to) ->
new Promise (resolve, reject) =>
return resolve() if files.length is 0
file = (files.splice 0, 1)[0].asFileHandle()
tof = "#{to}/#{file.basename}".asFileHandle()
file.read("binary")
.then (data) =>
tof.setCache(new Blob [data], { type: file.info.mime })
.write(file.info.mime)
.then (d) =>
@copy files, to
.then () -> resolve()
.catch (e) -> reject e
.catch (e) -> reject e
mkar: (meta) ->
@notify __("Preparing for release")
new Promise (r, e) =>
@import("os://scripts/jszip.min.js").then () ->
"#{meta.root}/build/debug".asFileHandle()
.read().then (d) ->
return e d.error if d.error
r d.result
.catch (ex) -> e ex
.catch (ex) -> e ex
.then (files) =>
new Promise (r, e) =>
zip = new JSZip()
fn = (list) =>
return r zip if list.length is 0
f = (list.splice 0, 1)[0].path.asFileHandle()
return fn list if f.type is "dir"
f.read("binary").then (d) =>
zip.file f.basename, d, { binary: true }
@notify __("add {0} to zip", f.basename)
fn list
.catch (ex) -> e ex
fn files
.then (zip) =>
zip.generateAsync({ type: "base64" }).then (data) =>
"#{meta.root}/build/release/#{meta.name}.zip"
.asFileHandle()
.setCache('data:application/zip;base64,' + data)
.write("base64").then (r) =>
return @error __("Cannot save the zip file: {0}", r.error) if r.error
@notify __("Package is generated in release folder")
.catch (e) => @error e.toString()
.catch (e) => @error e.toString()

View File

@ -13,7 +13,6 @@ class CodePad extends this.OS.GUI.BaseApplication
@currdir = @currfile.parent()
main: () ->
me = @
@extensions = {}
@fileview = @find("fileview")
@sidebar = @find("sidebar")
@ -27,12 +26,10 @@ class CodePad extends this.OS.GUI.BaseApplication
dir = path.asFileHandle() if typeof path is "string"
dir.read().then (d) ->
return reject d.error if d.error
me.currdir = dir
resolve d.result
@setup()
setup: () ->
me = @
ace.config.set('basePath', '/scripts/ace')
ace.require "ace/ext/language_tools"
@editor = ace.edit @find("datarea")
@ -50,56 +47,56 @@ class CodePad extends this.OS.GUI.BaseApplication
@editor.completers.push { getCompletions: ( editor, session, pos, prefix, callback ) -> }
@editor.getSession().setUseWrapMode true
@editormux = false
@editor.on "input", () ->
if me.editormux
me.editormux = false
@editor.on "input", () =>
if @editormux
@editormux = false
return false
if not me.currfile.dirty
me.currfile.dirty = true
me.currfile.text += "*"
me.tabbar.update()
@editor.getSession().selection.on "changeCursor", (e) ->
me.updateStatus()
if not @currfile.dirty
@currfile.dirty = true
@currfile.text += "*"
@tabbar.update()
@editor.getSession().selection.on "changeCursor", (e) =>
@updateStatus()
@tabbar.set "ontabselect", (e) ->
me.selecteTab $(e.data.item).index()
@tabbar.set "ontabclose", (e) ->
@tabbar.set "ontabselect", (e) =>
@selecteTab $(e.data.item).index()
@tabbar.set "ontabclose", (e) =>
it = e.data.item
return false unless it
return me.closeTab it unless it.get("data").dirty
me.openDialog("YesNoDialog", {
return @closeTab it unless it.get("data").dirty
@openDialog("YesNoDialog", {
title: __("Close tab"),
text: __("Close without saving ?")
}).then (d) ->
return me.closeTab it if d
me.editor.focus()
}).then (d) =>
return @closeTab it if d
@editor.focus()
return false
@fileview.set "onfileopen", (e) ->
@fileview.set "onfileopen", (e) =>
return if e.data.type is "dir"
me.openFile e.data.path.asFileHandle()
@openFile e.data.path.asFileHandle()
@fileview.set "onfileselect", (e) ->
@fileview.set "onfileselect", (e) =>
return unless e.data or e.data.type is "dir"
i = me.findTabByFile e.data.path.asFileHandle()
return me.tabbar.set "selected", i if i isnt -1
i = @findTabByFile e.data.path.asFileHandle()
return @tabbar.set "selected", i if i isnt -1
@on "resize", () -> me.editor.resize()
@on "focus", () -> me.editor.focus()
@on "resize", () => @editor.resize()
@on "focus", () => @editor.focus()
@spotlight = new CMDMenu __("Command palette")
@bindKey "ALT-P", () -> me.spotlight.run me
@find("datarea").contextmenuHandle = (e, m) ->
@bindKey "ALT-P", () => @spotlight.run @
@find("datarea").contextmenuHandle = (e, m) =>
m.set "items", [{
text: __("Command palete"),
onmenuselect: (e) ->
me.spotlight.run me
onmenuselect: (e) =>
@spotlight.run @
}]
m.show e
@bindKey "ALT-N", () -> me.menuAction "new"
@bindKey "ALT-O", () -> me.menuAction "open"
@bindKey "ALT-F", () -> me.menuAction "opendir"
@bindKey "CTRL-S", () -> me.menuAction "save"
@bindKey "ALT-W", () -> me.menuAction "saveas"
@bindKey "ALT-N", () => @menuAction "new"
@bindKey "ALT-O", () => @menuAction "open"
@bindKey "ALT-F", () => @menuAction "opendir"
@bindKey "CTRL-S", () => @menuAction "save"
@bindKey "ALT-W", () => @menuAction "saveas"
@loadExtensionMetaData()
@initCommandPalete()
@ -112,13 +109,13 @@ class CodePad extends this.OS.GUI.BaseApplication
i = @findTabByFile file
return @tabbar.set "selected", i if i isnt -1
return @newTab file if file.path.toString() is "Untitled"
me = @
file.read()
.then (d) ->
.then (d) =>
file.cache = d or ""
me.newTab file
.catch (e) ->
me.error __("Unable to open: {0}", file.path)
@newTab file
.catch (e) =>
@error __("Unable to open: {0}", file.path)
findTabByFile: (file) ->
lst = @tabbar.get "items"
@ -218,38 +215,35 @@ class CodePad extends this.OS.GUI.BaseApplication
@addAction CMDMenu.fromMenu @fileMenu()
loadExtensionMetaData: () ->
me = @
"#{@meta().path}/extensions.json"
.asFileHandle()
.read("json")
.then (d) ->
.then (d) =>
for ext in d
if me.extensions[ext.name]
me.extensions[ext.name].child = {}
me.extensions[ext.name].addAction v for v in ext.actions
if @extensions[ext.name]
@extensions[ext.name].child = {}
@extensions[ext.name].addAction v for v in ext.actions
else
me.extensions[ext.name] = new CMDMenu ext.text
me.extensions[ext.name].name = ext.name
me.extensions[ext.name].addAction v for v in ext.actions
me.spotlight.addAction me.extensions[ext.name]
me.extensions[ext.name].onchildselect (e) ->
me.loadAndRunExtensionAction e.data.item.get "data"
.catch (e) ->
me.error __("Cannot load extension meta data")
@extensions[ext.name] = new CMDMenu ext.text
@extensions[ext.name].name = ext.name
@extensions[ext.name].addAction v for v in ext.actions
@spotlight.addAction @extensions[ext.name]
@extensions[ext.name].onchildselect (e) =>
@loadAndRunExtensionAction e.data.item.get "data"
.catch (e) =>
@error __("Cannot load extension meta data")
runExtensionAction: (name, action) ->
me = @
return @error __("Unable to find extension: {0}", name) unless CodePad.extensions[name]
ext = new CodePad.extensions[name](me)
ext = new CodePad.extensions[name](@)
return @error __("Unable to find action: {0}", action) unless ext[action]
ext.preload()
.then () ->
ext[action]()
.catch (e) ->
me.error e.stack
.catch (e) =>
@error e.stack
loadAndRunExtensionAction: (data) ->
me = @
name = data.parent.name
action = data.name
#verify if the extension is load
@ -257,14 +251,13 @@ class CodePad extends this.OS.GUI.BaseApplication
#load the extension
path = "#{@meta().path}/extensions/#{name}.js"
@_api.requires path
.then () -> me.runExtensionAction name, action
.catch (e) ->
me.error __("unable to load extension: {}", name)
.then () => @runExtensionAction name, action
.catch (e) =>
@error __("unable to load extension: {}", name)
else
@runExtensionAction name, action
fileMenu: () ->
me = @
{
text: __("File"),
child: [
@ -274,35 +267,33 @@ class CodePad extends this.OS.GUI.BaseApplication
{ text: __("Save"), dataid: "save", shortcut: "C-S" },
{ text: __("Save as"), dataid: "saveas", shortcut: "A-W" }
],
onchildselect: (e, r) ->
me.menuAction e.data.item.get("data").dataid, r
onchildselect: (e, r) =>
@menuAction e.data.item.get("data").dataid, r
}
save: (file) ->
me = @
file.write("text/plain")
.then (d) ->
return me.error __("Error saving file {0}: {1}", file.basename, d.error) if d.error
.then (d) =>
return @error __("Error saving file {0}: {1}", file.basename, d.error) if d.error
file.dirty = false
file.text = file.basename
me.tabbar.update()
me.scheme.set "apptitle", "#{me.currfile.basename}"
.catch (e) -> me.error e.stack
@tabbar.update()
@scheme.set "apptitle", "#{@currfile.basename}"
.catch (e) => @error e.stack
saveAs: () ->
me = @
me.openDialog("FileDialog", {
@openDialog("FileDialog", {
title: __("Save as"),
file: me.currfile
file: @currfile
})
.then (f) ->
.then (f) =>
d = f.file.path.asFileHandle()
d = d.parent() if f.file.type is "file"
me.currfile.setPath "#{d.path}/#{f.name}"
me.save me.currfile
.catch (e) ->
me.error e.stack
@currfile.setPath "#{d.path}/#{f.name}"
@save @currfile
.catch (e) =>
@error e.stack
menuAction: (dataid, r) ->
me = @
@ -338,18 +329,16 @@ class CodePad extends this.OS.GUI.BaseApplication
cleanup: (evt) ->
dirties = ( v for v in @tabbar.get "items" when v.dirty )
return if dirties.length is 0
me = @
evt.preventDefault()
@.openDialog("YesNoDialog", {
title: "__(Quit)",
text: __("Ignore all {0} unsaved files ?", dirties.length)
}).then (d) ->
}).then (d) =>
if d
v.dirty = false for v in dirties
me.quit()
@quit()
menu: () ->
me = @
menu = [
@fileMenu()
{
@ -357,8 +346,8 @@ class CodePad extends this.OS.GUI.BaseApplication
child: [
{ text: "__(Command Palette)", dataid: "cmdpalette", shortcut: "A-P" }
],
onchildselect: (e, r) ->
me.spotlight.run me
onchildselect: (e, r) =>
@spotlight.run @
}
]
menu
@ -368,8 +357,10 @@ class CodePad.BaseExtension
constructor: (@app) ->
preload: () ->
dep = ( "#{@basedir()}/#{v}" for v in @dependencies())
Ant.OS.API.require dep
Ant.OS.API.require @dependencies()
import: (lib) ->
Ant.OS.API.requires lib
basedir: () ->
"#{@app.meta().path}/extensions"
@ -402,12 +393,11 @@ class CMDMenu
@
run: (root) ->
me = @
root.openDialog(new CommandPalette(), @)
.then (d) ->
.then (d) =>
data = d.data.item.get("data")
return data.run root if data.run
me.select d, root
@select d, root
CMDMenu.fromMenu = (mn) ->
m = new CMDMenu mn.text, mn.shortcut

View File

@ -14,15 +14,11 @@
},
{
"text": "__(Build and Run)",
"name": "build"
"name": "buildnrun"
},
{
"text": "__(Build release)",
"name": "release"
},
{
"text": "__(Build options)",
"name": "options"
}
]
}