do not lose setting on file access revoked

This commit is contained in:
Dmitry Vasilev
2023-06-22 15:56:14 +03:00
parent db1ba5958a
commit 1a93e5c493
5 changed files with 63 additions and 44 deletions

View File

@@ -824,10 +824,31 @@ const do_load_dir = (state, dir) => {
} }
} }
const load_dir = (state, dir) => { const apply_entrypoint_settings = (state, entrypoint_settings) => {
const blank_if_not_exists = key =>
state.files[entrypoint_settings[key]] == null
? ''
: entrypoint_settings[key]
const entrypoint = blank_if_not_exists('entrypoint')
const current_module = blank_if_not_exists('current_module')
const html_file = blank_if_not_exists('html_file')
return {
...state,
entrypoint,
current_module,
html_file,
}
}
const load_dir = (state, dir, entrypoint_settings) => {
// Clear parse cache and rerun code // Clear parse cache and rerun code
return run_code({ return run_code({
...do_load_dir(state, dir), ...apply_entrypoint_settings(
do_load_dir(state, dir),
entrypoint_settings,
),
// remove cache. We have to clear cache because imports of modules that are // remove cache. We have to clear cache because imports of modules that are
// not available because project_dir is not available have errors and the // not available because project_dir is not available have errors and the
// errors are cached // errors are cached
@@ -851,26 +872,16 @@ const open_run_window = (state, globals) => {
}) })
} }
const get_initial_state = state => { const get_initial_state = (state, entrypoint_settings) => {
const with_files = state.project_dir == null const with_files = state.project_dir == null
? state ? state
: do_load_dir(state, state.project_dir) : do_load_dir(state, state.project_dir)
const blank_if_not_exists = key => const with_settings = apply_entrypoint_settings(with_files, entrypoint_settings)
with_files.files[with_files[key]] == null
? ''
: with_files[key]
const entrypoint = blank_if_not_exists('entrypoint')
const current_module = blank_if_not_exists('current_module')
const html_file = blank_if_not_exists('html_file')
return { return {
...with_files, ...with_settings,
entrypoint, cursor_position_by_file: {[with_settings.current_module]: 0},
current_module,
html_file,
cursor_position_by_file: {[current_module]: 0},
} }
} }

View File

@@ -1,7 +1,7 @@
import {el} from './domutils.js' import {el} from './domutils.js'
import {map_find} from '../utils.js' import {map_find} from '../utils.js'
import {load_dir, create_file} from '../filesystem.js' import {load_dir, create_file} from '../filesystem.js'
import {exec, get_state} from '../index.js' import {exec, get_state, open_directory} from '../index.js'
export class Files { export class Files {
constructor(ui) { constructor(ui) {
@@ -10,15 +10,6 @@ export class Files {
this.render(get_state()) this.render(get_state())
} }
open_directory() {
if(globalThis.showDirectoryPicker == null) {
throw new Error('Your browser is not supporting File System Access API')
}
load_dir(true).then(dir => {
exec('load_dir', dir)
})
}
render(state) { render(state) {
if(state.project_dir == null) { if(state.project_dir == null) {
this.el.innerHTML = '' this.el.innerHTML = ''
@@ -26,7 +17,7 @@ export class Files {
el('div', 'allow_file_access', el('div', 'allow_file_access',
el('a', { el('a', {
href: 'javascript:void(0)', href: 'javascript:void(0)',
click: this.open_directory.bind(this), click: open_directory,
}, },
`Allow access to local project folder`, `Allow access to local project folder`,
), ),

View File

@@ -126,28 +126,37 @@ export const reload_run_window = state => {
const read_modules = async () => { const read_modules = async () => {
const default_module = {'': localStorage.code || EXAMPLE} const default_module = {'': localStorage.code || EXAMPLE}
const current = {
// TODO fix when there are no such modules anymore
current_module: localStorage.current_module ?? '',
entrypoint: localStorage.entrypoint ?? '',
html_file: localStorage.html_file ?? '',
}
const project_dir = await load_dir(false) const project_dir = await load_dir(false)
if(project_dir == null) { if(project_dir == null) {
// Single anonymous module // Single anonymous module
return { return {
...current,
files: default_module, files: default_module,
} }
} else { } else {
return { return {
...current,
project_dir, project_dir,
files: default_module, files: default_module,
} }
} }
} }
const get_entrypoint_settings = () => ({
current_module: localStorage.current_module ?? '',
entrypoint: localStorage.entrypoint ?? '',
html_file: localStorage.html_file ?? '',
})
export const open_directory = () => {
if(globalThis.showDirectoryPicker == null) {
throw new Error('Your browser is not supporting File System Access API')
}
load_dir(true).then(dir => {
exec('load_dir', dir, get_entrypoint_settings())
})
}
let COMMANDS let COMMANDS
let ui let ui
let state let state
@@ -159,10 +168,14 @@ export const init = (container, _COMMANDS) => {
read_modules().then(initial_state => { read_modules().then(initial_state => {
state = COMMANDS.get_initial_state({ state = COMMANDS.get_initial_state(
...initial_state, {
on_deferred_call: (...args) => exec('on_deferred_call', ...args) ...initial_state,
}) on_deferred_call: (...args) => exec('on_deferred_call', ...args)
},
get_entrypoint_settings(),
)
// Expose state for debugging // Expose state for debugging
globalThis.__state = state globalThis.__state = state

View File

@@ -69,7 +69,8 @@ console.time('run')
const i = test_initial_state( const i = test_initial_state(
{}, // files {}, // files
{project_dir: dir, entrypoint: 'test/run.js'} {entrypoint: 'test/run.js'},
{project_dir: dir}
) )
assert_equal(i.loading_external_imports_state != null, true) assert_equal(i.loading_external_imports_state != null, true)

View File

@@ -98,15 +98,18 @@ export const assert_code_error_async = async (codestring, error) => {
assert_equal(result.error, error) assert_equal(result.error, error)
} }
export const test_initial_state = (code, state) => { export const test_initial_state = (code, entrypoint_settings, other) => {
return COMMANDS.open_run_window( return COMMANDS.open_run_window(
COMMANDS.get_initial_state( COMMANDS.get_initial_state(
{ {
files: typeof(code) == 'object' ? code : { '' : code}, files: typeof(code) == 'object' ? code : { '' : code},
...other
},
{
entrypoint: '', entrypoint: '',
current_module: '', current_module: '',
...state, ...entrypoint_settings,
}, }
), ),
new Set(Object.getOwnPropertyNames(globalThis.run_window)) new Set(Object.getOwnPropertyNames(globalThis.run_window))
) )
@@ -150,7 +153,7 @@ export const test_deferred_calls_state = code => {
} }
`))() `))()
const state = test_initial_state(code, { on_deferred_call }) const state = test_initial_state(code, null, { on_deferred_call })
return { return {
state, state,