feature: client: add DIR_DIST

This commit is contained in:
coderaiser 2023-01-30 13:15:49 +02:00
parent d7e43d8a18
commit bdddc5752d
4 changed files with 91 additions and 96 deletions

View file

@ -59,8 +59,6 @@ function CloudCmdProto(DOM) {
this.prefix = '';
this.prefixSocket = '';
this.prefixURL = '';
this.DIR_CLIENT = '/dist/';
this.DIR_MODULES = this.DIR_CLIENT + 'modules/';
this.MIN_ONE_PANEL_WIDTH = 1155;
this.HOST = location.origin || location.protocol + '//' + location.host;
@ -111,6 +109,8 @@ function CloudCmdProto(DOM) {
CloudCmd.prefix = prefix;
CloudCmd.prefixURL = `${prefix}${apiURL}`;
CloudCmd.prefixSocket = config.prefixSocket;
CloudCmd.DIR_DIST = `${prefix}/dist`;
CloudCmd.DIR_MODULES = `${this.DIR_DIST}/modules`;
CloudCmd.config = (key) => config[key];
CloudCmd.config.if = currify((key, fn, a) => config[key] && fn(a));
@ -130,7 +130,7 @@ function CloudCmdProto(DOM) {
CloudCmd.MIN_ONE_PANEL_WIDTH = Infinity;
if (!document.body.scrollIntoViewIfNeeded)
await load.js(prefix + CloudCmd.DIR_MODULES + 'polyfill.js');
await load.js(`${CloudCmd.DIR_MODULES}/polyfill.js`);
await initModules();
await baseInit();

View file

@ -25,25 +25,24 @@ module.exports = function loadModule(params) {
if (CloudCmd[name])
return;
CloudCmd[name] = () => {
CloudCmd[name] = async () => {
exec(doBefore);
const {prefix} = CloudCmd;
const pathFull = prefix + CloudCmd.DIR_MODULES + path + '.js';
const {DIR_MODULES} = CloudCmd;
const pathFull = `${DIR_MODULES}/${path}.js`;
return loadJS(pathFull).then(async () => {
const newModule = async (f) => f && f();
const module = CloudCmd[name];
await loadJS(pathFull);
const newModule = async (f) => f && f();
const module = CloudCmd[name];
Object.assign(newModule, module);
Object.assign(newModule, module);
CloudCmd[name] = newModule;
CloudCmd[name] = newModule;
CloudCmd.log('init', name);
CloudCmd.log('init', name);
await module.init();
await module.init();
return newModule;
});
return newModule;
};
CloudCmd[name].show = async (...args) => {

View file

@ -31,7 +31,7 @@ const ConfigView = {
module.exports.init = async () => {
const element = create();
await CloudCmd.View();
await loadFiles(element);
};
@ -44,9 +44,9 @@ function create() {
'font-family: "Droid Sans Mono";',
notAppend: true,
});
Element = element;
return element;
}
@ -60,30 +60,26 @@ function initConfig(options = {}) {
...options,
...ConfigView,
};
if (!options.afterShow)
return config;
checkFn('options.afterShow', options.afterShow);
const afterShow = {
config,
};
config.afterShow = () => {
afterShow();
ConfigView.afterShow();
options.afterShow();
};
return config;
}
module.exports.show = (options) => {
if (Loading)
return;
CloudCmd.View.show(Element, initConfig(options));
getEditor()
.setOptions({
fontSize: 16,
@ -107,11 +103,11 @@ const loadFiles = async (element) => {
const socketPath = CloudCmd.prefix;
const prefixSocket = `${CloudCmd.prefixSocket}/${EditorName}`;
const url = `${prefix}/${EditorName}.js`;
time(Name + ' load');
await loadJS(url);
const word = promisify(window[EditorName]);
const [ed] = await tryToCatch(word, element, {
maxSize,
@ -119,7 +115,7 @@ const loadFiles = async (element) => {
prefixSocket,
socketPath,
});
timeEnd(Name + ' load');
editor = ed;
Loading = false;

View file

@ -64,20 +64,20 @@ const Config = {
Images.hide();
Key.unsetBind();
},
beforeClose: () => {
Events.rmKey(listener);
Key.setBind();
},
afterShow: () => {
El.focus();
},
onOverlayClick,
afterClose: noop,
autoSize: false,
helpers: {
title: {},
},
@ -86,62 +86,62 @@ module.exports._Config = Config;
module.exports.init = async () => {
await loadAll();
const events = [
'click',
'contextmenu',
];
events.forEach(addEvent(Overlay, onOverlayClick));
};
async function show(data, options = {}) {
const prefixURL = CloudCmd.prefixURL + FS;
if (Loading)
return;
if (!options || options.bindKeys !== false)
Events.addKey(listener);
El = createElement('div', {
className: 'view',
notAppend: true,
});
El.tabIndex = 0;
if (data) {
if (isArray(data))
El.append(...data);
else
El.append(data);
modal.open(El, initConfig(options));
return;
}
Images.show.load();
const path = prefixURL + Info.path;
const type = options.raw ? '' : await getType(path);
switch(type) {
default:
return await viewFile();
case 'markdown':
return await CloudCmd.Markdown.show(Info.path);
case 'html':
return viewHtml(path);
case 'image':
return viewImage(Info.path, prefixURL);
case 'media':
return await viewMedia(path);
case 'pdf':
return viewPDF(path);
}
@ -154,11 +154,11 @@ function createIframe(src) {
width: '100%',
height: '100%',
});
element.addEventListener('load', () => {
element.contentWindow.addEventListener('keydown', listener);
});
return element;
}
@ -169,21 +169,21 @@ function viewHtml(src) {
function viewPDF(src) {
const element = createIframe(src);
const options = assign({}, Config);
if (CloudCmd.config('showFileName'))
options.title = Info.name;
modal.open(element, options);
}
async function viewMedia(path) {
const [e, element] = await getMediaElement(path);
if (e)
return alert(e);
const allConfig = {
...Config,
...{
@ -195,22 +195,22 @@ async function viewMedia(path) {
},
},
};
modal.open(element, allConfig);
}
async function viewFile() {
const [error, data] = await Info.getData();
if (error)
return Images.hide();
const element = document.createTextNode(data);
const options = Config;
if (CloudCmd.config('showFileName'))
options.title = Info.name;
El.append(element);
modal.open(El, options);
}
@ -220,25 +220,25 @@ const copy = (a) => assign({}, a);
module.exports._initConfig = initConfig;
function initConfig(options) {
const config = copy(Config);
if (!options)
return config;
const names = Object.keys(options);
for (const name of names) {
const isConfig = Boolean(config[name]);
const item = options[name];
if (!isFn(item) || !isConfig) {
config[name] = options[name];
continue;
}
const fn = config[name];
config[name] = series(fn, item);
}
return config;
}
@ -252,14 +252,14 @@ function viewImage(path, prefixURL) {
href: `${prefixURL}${path}`,
title: encode(basename(path)),
});
const names = Info.files
.map(DOM.getCurrentPath)
.filter(isSupportedImage);
const titles = names
.map(makeTitle);
const index = names.indexOf(Info.path);
const imageConfig = {
index,
@ -270,41 +270,41 @@ function viewImage(path, prefixURL) {
title : {},
},
};
const config = {
...Config,
...imageConfig,
};
modal.open(titles, config);
}
async function getMediaElement(src) {
check(src);
const [error, template] = await tryToCatch(Files.get, 'view/media-tmpl');
if (error)
return [error];
const {name} = Info;
if (!TemplateAudio)
TemplateAudio = template;
const is = isAudio(name);
const type = is ? 'audio' : 'video';
const innerHTML = rendy(TemplateAudio, {
src,
type,
name,
});
const element = createElement('div', {
innerHTML,
});
return [null, element];
}
@ -318,12 +318,12 @@ function check(src) {
* @callback - executes, when everything loaded
*/
async function loadAll() {
const {prefix} = CloudCmd;
const {DIR_DIST} = CloudCmd;
time(Name + ' load');
Loading = true;
await loadCSS(`${prefix}/dist/view.css`);
await loadCSS(`${DIR_DIST}/view.css`);
Loading = false;
}
@ -332,32 +332,32 @@ function onOverlayClick(event) {
x: event.clientX,
y: event.clientY,
};
setCurrentByPosition(position);
}
function setCurrentByPosition(position) {
const element = DOM.getCurrentByPosition(position);
if (!element)
return;
const {
files,
filesPassive,
} = Info;
const isFiles = files.includes(element);
const isFilesPassive = filesPassive.includes(element);
if (!isFiles && !isFilesPassive)
return;
const isCurrent = DOM.isCurrentFile(element);
if (isCurrent)
return;
DOM.setCurrentFile(element);
}