Skip to content

Commit 1f17487

Browse files
committed
feature: client: view: migrate to ESM
1 parent 7173f6c commit 1f17487

6 files changed

Lines changed: 54 additions & 68 deletions

File tree

.webpack/js.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default {
125125
[`${modules}/edit-names`]: `${dirModules}/edit-names.js`,
126126
[`${modules}/edit-names-vim`]: `${dirModules}/edit-names-vim.js`,
127127
[`${modules}/menu`]: `${dirModules}/menu/index.mjs`,
128-
[`${modules}/view`]: `${dirModules}/view/index.js`,
128+
[`${modules}/view`]: `${dirModules}/view/index.mjs`,
129129
[`${modules}/help`]: `${dirModules}/help.js`,
130130
[`${modules}/markdown`]: `${dirModules}/markdown.js`,
131131
[`${modules}/config`]: `${dirModules}/config/index.mjs`,
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
'use strict';
1+
import currify from 'currify';
22

3-
const currify = require('currify');
43
const testRegExp = currify((name, reg) => reg.test(name));
54
const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i');
65

76
const isPDF = (a) => /\.pdf$/i.test(a);
87
const isHTML = (a) => a.endsWith('.html');
98
const isMarkdown = (a) => /.\.md$/.test(a);
109

11-
module.exports = (name) => {
10+
export default (name) => {
1211
if (isPDF(name))
1312
return 'pdf';
1413

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
11
/* global CloudCmd, DOM */
2-
3-
'use strict';
4-
5-
const CloudCmd = globalThis.CloudCmd || {};
6-
const DOM = globalThis.DOM || {};
7-
8-
require('../../../css/view.css');
9-
10-
const rendy = require('rendy');
11-
const currify = require('currify');
12-
const wraptile = require('wraptile');
13-
const {tryToCatch} = require('try-to-catch');
14-
const load = require('load.js');
15-
16-
const _modal = require('@cloudcmd/modal');
17-
const _createElement = require('@cloudcmd/create-element');
18-
19-
const {time} = require('#common/util');
20-
const {FS} = require('../../../common/cloudfunc.mjs');
21-
22-
const {
2+
import rendy from 'rendy';
3+
import currify from 'currify';
4+
import wraptile from 'wraptile';
5+
import {tryToCatch} from 'try-to-catch';
6+
import load from 'load.js';
7+
import * as _modal from '@cloudcmd/modal';
8+
import _createElement from '@cloudcmd/create-element';
9+
import {time} from '#common/util';
10+
import * as Files from '#dom/files';
11+
import * as Events from '#dom/events';
12+
import '../../../css/view.css';
13+
import {FS} from '../../../common/cloudfunc.mjs';
14+
import {
2315
isImage,
2416
isAudio,
2517
getType,
26-
} = require('./types');
27-
28-
const Files = require('#dom/files');
29-
const Events = require('#dom/events');
30-
const Images = require('../../dom/images.mjs');
18+
} from './types.mjs';
19+
import * as Images from '../../dom/images.mjs';
20+
import {encode} from '../../../common/entity.js';
3121

32-
const {encode} = require('../../../common/entity');
22+
const CloudCmd = globalThis.CloudCmd || {};
23+
const DOM = globalThis.DOM || {};
3324
const isString = (a) => typeof a === 'string';
3425
const {assign} = Object;
3526
const {isArray} = Array;
@@ -47,14 +38,15 @@ const addEvent = lifo(Events.add);
4738

4839
const loadCSS = load.css;
4940

50-
module.exports.show = show;
51-
module.exports.hide = hide;
52-
5341
let Loading = false;
5442

5543
const Name = 'View';
5644

57-
CloudCmd[Name] = module.exports;
45+
CloudCmd[Name] = {
46+
init,
47+
show,
48+
hide,
49+
};
5850

5951
const Info = DOM.CurrentInfo;
6052
const {Key} = CloudCmd;
@@ -91,9 +83,9 @@ const Config = {
9183
},
9284
};
9385

94-
module.exports._Config = Config;
86+
export const _Config = Config;
9587

96-
module.exports.init = async () => {
88+
export async function init() {
9789
await loadAll();
9890

9991
const events = [
@@ -105,9 +97,9 @@ module.exports.init = async () => {
10597
Overlay,
10698
onOverlayClick,
10799
));
108-
};
100+
}
109101

110-
async function show(data, options = {}) {
102+
export async function show(data, options = {}) {
111103
const prefixURL = CloudCmd.prefixURL + FS;
112104

113105
if (Loading)
@@ -159,7 +151,8 @@ async function show(data, options = {}) {
159151
}
160152
}
161153

162-
module.exports._createIframe = createIframe;
154+
export const _createIframe = createIframe;
155+
163156
function createIframe(src, overrides = {}) {
164157
const {
165158
createElement = _createElement,
@@ -178,7 +171,8 @@ function createIframe(src, overrides = {}) {
178171
return element;
179172
}
180173

181-
module.exports._viewHtml = viewHtml;
174+
export const _viewHtml = viewHtml;
175+
182176
function viewHtml(src, overrides = {}) {
183177
const {modal = _modal} = overrides;
184178
modal.open(createIframe(src), Config);
@@ -234,7 +228,8 @@ async function viewFile() {
234228

235229
const copy = (a) => assign({}, a);
236230

237-
module.exports._initConfig = initConfig;
231+
export const _initConfig = initConfig;
232+
238233
function initConfig(options) {
239234
const config = copy(Config);
240235

@@ -260,7 +255,7 @@ function initConfig(options) {
260255
return config;
261256
}
262257

263-
function hide() {
258+
export function hide() {
264259
_modal.close();
265260
}
266261

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
'use strict';
2-
3-
require('css-modules-require-hook/preset');
4-
5-
const autoGlobals = require('auto-globals');
6-
const {stub} = require('@cloudcmd/stub');
7-
const {test: tape} = require('supertape');
8-
const test = autoGlobals(tape);
9-
10-
const {
1+
import autoGlobals from 'auto-globals';
2+
import {stub} from '@cloudcmd/stub';
3+
import {test as tape} from 'supertape';
4+
import {
115
_initConfig,
126
_viewHtml,
137
_Config,
148
_createIframe,
15-
} = require('.');
9+
} from './index.mjs';
10+
11+
const test = autoGlobals(tape);
1612

1713
test('cloudcmd: client: view: initConfig', (t) => {
1814
let config;
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
'use strict';
1+
import {extname} from 'node:path';
2+
import currify from 'currify';
3+
4+
export const isAudio = (name) => /\.(mp3|ogg|m4a)$/i.test(name);
25

3-
const {extname} = require('node:path');
4-
const currify = require('currify');
5-
const isAudio = (name) => /\.(mp3|ogg|m4a)$/i.test(name);
66
const testRegExp = currify((name, reg) => reg.test(name));
77
const getRegExp = (ext) => RegExp(`\\.${ext}$`, 'i');
88

99
const isPDF = (a) => /\.pdf$/i.test(a);
1010
const isHTML = (a) => a.endsWith('.html');
1111
const isMarkdown = (a) => /.\.md$/.test(a);
1212

13-
module.exports.getType = async (path) => {
13+
export const getType = async (path) => {
1414
const ext = extname(path);
1515

1616
if (!ext)
@@ -32,8 +32,7 @@ module.exports.getType = async (path) => {
3232
return 'markdown';
3333
};
3434

35-
module.exports.isImage = isImage;
36-
function isImage(name) {
35+
export function isImage(name) {
3736
const images = [
3837
'jp(e|g|eg)',
3938
'gif',
@@ -53,13 +52,12 @@ function isMedia(name) {
5352
return isAudio(name) || isVideo(name);
5453
}
5554

56-
module.exports.isAudio = isAudio;
57-
5855
function isVideo(name) {
5956
return /\.(mp4|avi|webm)$/i.test(name);
6057
}
6158

62-
module.exports._detectType = detectType;
59+
export const _detectType = detectType;
60+
6361
async function detectType(path) {
6462
const {headers} = await fetch(path, {
6563
method: 'HEAD',
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const {test, stub} = require('supertape');
4-
const {isAudio, _detectType} = require('./types');
1+
import {test, stub} from 'supertape';
2+
import {isAudio, _detectType} from './types.mjs';
53

64
test('cloudcmd: client: view: types: isAudio', (t) => {
75
const result = isAudio('hello.mp3');

0 commit comments

Comments
 (0)