Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/promignis/knack into wind…
Browse files Browse the repository at this point in the history
…ows-branch
  • Loading branch information
revant117 committed Oct 9, 2018
2 parents d2bc2a9 + a11db56 commit c8ca50e
Show file tree
Hide file tree
Showing 14 changed files with 236,266 additions and 15 deletions.
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ It is under development.

Current usage(**it will change**)

html files in views folder (will pick index.html default)
css files in styles folder
js files in js folder
image files in images folder
html/svg files in views folder (will pick index.html by default)
css files in styles folder
js files in js folder
image files in images folder

### Inject
```js
Expand Down Expand Up @@ -41,6 +41,29 @@ Save file with `fileData`
_runtime.saveFile(fileData)
```

### Fuzzy match
Do fuzzy match over a any dictionary
```js
// dict, word, levenshtein_distance, callback

_runtime.fuzzyMatch(["asd", "abc"], "ab", 1, (results) => {
// returns array of results
// in this case "abc" as 1 distance away from "ab"
})
```

### Examples
Fuzzy search on files

```js
_runtime.getFileWalker("../", (fileList) => {
let fileDict = JSON.parse(fileList).map(file => file.name)
_runtime.fuzzyMatch(fileDict, "main.g", 3, (fuzzyResults) => {

})
})
```

## OSX

### Build
Expand Down
44 changes: 36 additions & 8 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/promignis/knack/fs"
"github.com/promignis/knack/fuzzy"
"github.com/promignis/knack/persistance"
"github.com/promignis/knack/utils"

Expand All @@ -30,6 +31,7 @@ func HandleRPC(w webview.WebView, data string) {

// TODO: standardize all these actions
// and format
// find better way to add than switch case
switch fnType {
case "alert":
w.Dialog(webview.DialogTypeAlert, 0, "title", ffiData["msg"].(string))
Expand All @@ -49,13 +51,7 @@ func HandleRPC(w webview.WebView, data string) {
// transfer binary data as natively as possible
// profile for speed
data := string(fs.GetFileData(filePath))
callbackId := int(ffiData["callbackId"].(float64))
args := []string{data}
cbData := &CallbackData{
callbackId,
args,
}
ResolveJsCallback(w, cbData)
HandleCallback(w, ffiData, []string{data})
case "open_dir":
directoryPath := w.Dialog(webview.DialogTypeOpen, webview.DialogFlagDirectory, "Open directory", "")
_ = directoryPath
Expand All @@ -70,6 +66,38 @@ func HandleRPC(w webview.WebView, data string) {
imageData := fs.FileState[imageName].Data()
base64Img := base64.StdEncoding.EncodeToString(imageData)
RunJsInWebview(w, InjectImage(base64Img, imageId))
case "file_walker":
filePath := ffiData["filePath"].(string)
fileList := fs.GetFileList(filePath)
stringified, err := json.Marshal(fileList)
utils.CheckErr(err)

fileData := []string{string(stringified)}

HandleCallback(w, ffiData, fileData)
case "file_stat":
filePath := ffiData["filePath"].(string)
stat := fs.GetFileStat(filePath)
// centralize json marshalling and instead of crash due to wrong data being sent
// better errors
stringifiedStat, err := json.Marshal(stat)
utils.CheckErr(err)
args := []string{string(stringifiedStat)}
HandleCallback(w, ffiData, args)
case "fuzzy_match":
dict := ffiData["dict"].(string)
var nDict []string
err = json.Unmarshal([]byte(dict), &nDict)
utils.CheckErr(err)
word := ffiData["word"].(string)
distance := int(ffiData["distance"].(float64))

candidates := fuzzy.GetFuzzyCandidates(word, nDict, distance)

stringified, err := json.Marshal(candidates)
utils.CheckErr(err)
args := []string{string(stringified)}
HandleCallback(w, ffiData, args)
case "set_to_file":
filename := ffiData["filename"].(string)
stringifiedJson := ffiData["stringifiedJson"].(string)
Expand All @@ -83,7 +111,7 @@ func HandleRPC(w webview.WebView, data string) {
callbackId,
args,
}
ResolveJsCallback(w, cbData)
HandleCallback(w, ffiData, args)
default:
fmt.Printf("No such action %s", fnType)
}
Expand Down
12 changes: 11 additions & 1 deletion bridge/js_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ import (
"github.com/zserge/webview"
)

func HandleCallback(w webview.WebView, ffiData map[string]interface{}, args []string) {
callbackId := int(ffiData["callbackId"].(float64))
cbData := &CallbackData{
callbackId,
args,
}
// maybe put log for helping debug with callback and data sent
resolveJsCallback(w, cbData)
}

// send a value to a js callback
func ResolveJsCallback(w webview.WebView, cbData *CallbackData) {
func resolveJsCallback(w webview.WebView, cbData *CallbackData) {
callbackStrData, err := json.Marshal(cbData)
utils.CheckErr(err)
js := fmt.Sprintf(`_runtime.resolveCallback("%s")`, template.JSEscapeString(string(callbackStrData)))
Expand Down
Loading

0 comments on commit c8ca50e

Please sign in to comment.