Some tweaks, directory viewing

This commit is contained in:
Dakedres 2023-12-25 21:21:42 -05:00
parent 030f0e6438
commit 4648240842
6 changed files with 124 additions and 24 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
# Qwe

View File

@ -6,15 +6,21 @@ const handleNeutralinoError = err => {
throw new Error(err.code + ': ' + err.message)
}
const mapDirent = ({ entry, type }) => ({
name: entry,
type
})
window.Platform = {
//
// Paths
//
dirname(path) {
let index = path.lastIndexOf('/')
let normalPath = this.normalize(path)
let index = normalPath.lastIndexOf('/')
return index === -1 ? '' : path.slice(0, index)
return index === -1 ? '' : normalPath.slice(0, index)
},
filename(path) {
@ -31,7 +37,13 @@ window.Platform = {
},
join(...args) {
return args.join('/')
let parts = []
for(let arg of args) {
parts = parts.concat(arg.split('/'))
}
return this.normalizePathFromParts(parts)
},
// TODO: support non-posix
@ -39,6 +51,38 @@ window.Platform = {
return path.charAt(0) === '/'
},
normalize(path) {
return this.normalizePathFromParts(path.split('/'))
},
normalizePathFromParts(parts) {
// let newPath = path !== '/' && path.endsWith('/') ?
// path.slice(0, -1) :
// path
let out = []
let skips = 0
for(let i = parts.length - 1; i >= 0; i--) {
let part = parts[i]
if(part.length == 0 || part === '.') {
continue
} else if(part == '..') {
skips++
continue
} else if(skips == 0) {
out.unshift(part)
} else {
skips--
}
}
console.log(out)
return '/' + out.join('/')
},
//
// FS
//
@ -66,6 +110,12 @@ window.Platform = {
.catch(handleNeutralinoError)
},
readdir(path) {
return Neutralino.filesystem.readDirectory(path)
.catch(handleNeutralinoError)
.then(dirents => dirents.map(mapDirent))
},
//
// OS
//

View File

@ -3,6 +3,11 @@
Needed to
```sudo apt install libsoup-2.4-dev libwebkit2gtk-4.0-dev libjavascriptcoregtk-4.0-dev```
On void:
```
sudo xbps-install webkit2gtk-devel
```
---
# Functionality

View File

@ -15,7 +15,8 @@
"build-deps": "cd lib && rollup -c",
"dev": "neu run",
"build": "neu build",
"update": "neu update"
"update": "neu update",
"install:neu": "rm src/lib/platform.js && ln lib/platforms/neutralino.js src/lib/platforms.js && ln build/neutralino.js src/lib/neutralino.js"
},
"type": "module"
}

View File

@ -12,7 +12,8 @@ binds: {
reload: 'Ctrl-Escape',
editPath: 'Ctrl-r',
save: 'Ctrl-s',
open: 'Ctrl-o'
open: 'Ctrl-o',
openDirectory: 'Ctrl-l'
},
languages: [

View File

@ -25,16 +25,10 @@ function Editor() {
this.view.focus()
}
// Opens a directory or a file
this.open = path => {
}
this.openDocument = async (path) => {
this.doc = await Doc.open(path)
.catch(err => `Failed to open "${path}": ${err}`)
this.view.setState(await this.doc.createState())
this.doc.setView(this.view)
await Doc.open(path)
.then(doc => this.setDoc(doc))
.catch(err => console.error(`Failed to open "${path}": ${err}`))
}
this.saveDocument = () => {
@ -51,6 +45,23 @@ function Editor() {
statusbar.updateFilename()
}
this.setDoc = async (doc, bind) => {
this.doc = doc
statusbar.updateFilename()
this.view.setState(await this.doc.createState())
this.doc.setView(this.view)
}
this.openDirectory = async (path) => {
let fileList = (await Platform.readdir(path))
.filter(dirent => dirent.name != '.')
.map(dirent => dirent.type == 'FILE' ? dirent.name : dirent.name + '/')
.join('\n')
let doc = new Doc(fileList, Platform.join(path, '~' + Platform.filename(path)))
editor.setDoc(doc)
}
// this.updateFilename = () => {
// this.doc.path = Platform.dirname(this.doc.path) + '/' + statusbar.filename.value
// }
@ -79,11 +90,11 @@ function Statusbar() {
this.filename.value = path
this.filename.select()
this.filename.selectionStart = path.length - Platform.filename(path).length
this.filename.selectionStart = path.length - getPathName(path).length
}
this.updateFilename = () => {
this.filename.value = Platform.filename(editor.doc.getPath())
this.filename.value = getPathName(editor.doc.getPath())
}
this.updateWorkspace = () => {
@ -109,6 +120,16 @@ function Statusbar() {
}
}
const getPathName = path => {
if(path.endsWith('/')) {
let index = path.lastIndexOf('/', path.length - 2) + 1
return path.slice(index)
} else {
return Platform.filename(path)
}
}
this.element = document.createElement('nav')
this.filename = document.createElement('input')
this.workspace = document.createElement('input')
@ -150,6 +171,7 @@ function Doc(content, initialPath) {
cm.rectangularSelection(),
cm.crosshairCursor(),
cm.highlightSelectionMatches(),
cm.history(),
Keymaps.default
]
@ -217,13 +239,9 @@ function Doc(content, initialPath) {
}
Doc.open = async (path) => {
console.log('Opening ' + path)
let content = await Platform.readText(path)
.catch(console.error)
console.log(content)
return new Doc(content ?? '', path)
}
@ -238,6 +256,7 @@ const Keymaps = {
...cm.searchKeymap,
...cm.foldKeymap,
...cm.lintKeymap,
...cm.historyKeymap,
// Alt completion keymap that merges with tab to indent
{ key: "Ctrl-Space", run: cm.startCompletion },
@ -266,8 +285,7 @@ const Keymaps = {
if(!mainSelection.empty) {
let path = state.sliceDoc(mainSelection.from, mainSelection.to).trim()
console.log(path)
let isDirectory = path.endsWith('/')
editor.setDocPath(
Platform.absolute(path) ?
@ -275,10 +293,18 @@ const Keymaps = {
Platform.join(Platform.dirname(editor.doc.getPath()), path)
)
statusbar.updateFilename()
path = editor.doc.getPath()
if(isDirectory) {
editor.openDirectory(path)
} else {
editor.openDocument(path)
}
return
}
editor.openDocument(editor.doc.getPath())
editor.openDirectory(editor.doc.getPath())
}
},
{
@ -287,6 +313,22 @@ const Keymaps = {
editor.saveDocument()
}
},
{
key: config.binds.openDirectory,
async run() {
let path = editor.doc.getPath()
if(Platform.filename(path).startsWith('~'))
path = Platform.join(path, '../../')
else
path = Platform.dirname(path)
// path = path.endsWith('/') ? path.slice(0, -1) : path
console.log(path)
editor.openDirectory(path)
}
},
{
key: config.binds.reload,