Skip to content

Commit 18a3ade

Browse files
author
Bill Yosafat
committed
update README, build scripts, bump Ace, preview message channel, environment setup
1 parent 9dcda16 commit 18a3ade

File tree

12 files changed

+1114
-245
lines changed

12 files changed

+1114
-245
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# node_modules cloned assets
22
codetmp/assets/js/ace-builds@*
3+
codetmp/assets/js/ace-builds
34

45
# Logs
56
logs

README.md

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,55 +56,68 @@ Both classes and inline styles will be concatenated into single attribute (class
5656
Read more at https://github.com/tmpmachine/divless-html.
5757

5858
## Development
59-
There are two main directories : `/codetmp` for the IDE, and `/cpreview` for file preview (works like file hosting).
60-
61-
For editor-only development (`/codetmp`), simply grab a clone of this repo and open `codetmp/index.html` without local server.
62-
63-
You will need local server to test file preview and accessing 3rd party APIs (Google Drive sync, deploy Firebase Hosting, Git clone).
59+
This project is developed using plain JavaScript, so you might have a hard time to navigate around the codebase. I don't plan to change this into TS or anything, sorry! I want to see how far can this project go with plain JavaScript.
6460

65-
Developing `/cpreview` requires both projects to run on the same host or domain. The communication is done through iframes by claiming service worker registration client.
61+
There are two main directories : `/codetmp` for the IDE, and `/cpreview` for file preview (works like file hosting).
6662

67-
### Running and Building The Project
68-
1. Initial setup.
69-
```
70-
# install dependencies
71-
npm install
63+
### Building and Running The Project
64+
I host the project on firebase hosting, but you can run the project just fine without installing [firebase-tools](https://www.npmjs.com/package/firebase-tools). I've prepared a server setup using [express](https://www.npmjs.com/package/express).
7265

73-
# clone ace editor build files to codetmp/assets/js/ace-builds@version
74-
# You may need to adjust the versioning in codetmp/index.js (see ACE_CDN_BASEPATH), also in codetmp/manifest-cache.json for offline mode caching.
75-
npm run setup-dev
76-
```
77-
2. Serving the projects. I use `firebase-tools`, but you can use any local web server just fine. Open `firebase.dev.json` for firebase hosting configuration.
66+
Run below commands for initial setup and running the servers :
7867
```
68+
npm i
69+
npm run setup
7970
npm run dev
8071
```
81-
3. Deploying the projects to firebase (see configuration at `firebase.prod.json`) :
72+
73+
You should get the following:
8274
```
83-
npm run deploy
75+
Servers running at:
76+
codetmp: http://localhost:8000/
77+
cpreview: http://localhost:8001/
78+
cpreview: http://localhost:8002/
8479
```
8580

86-
#### Minimized Build
87-
To build minimized files for `/codetmp` project, run :
81+
82+
### Publishing The Project
83+
84+
Both **codetmp** and **cpreview** utilize service worker to enable offline access. You'll need to update the cache counter on both service workers to trigger application update on end user, otherwise they'll need to manually clear the app cache.
85+
86+
1. codetmp/sw.js : increase **unique numer** counter by +1 on each update on codetmp.
87+
2. cpreview/sw.js : increase `cacheVersion` by +1 on each update on cpreview.
88+
89+
> Notes : Codetmp7 editor has an option to clear and update application cache in **Settings** menu.
90+
91+
#### Building Minimized Project
92+
93+
If you need a minimized version of **codetmp**, run the following :
8894
```
8995
npm run build
9096
```
9197
Minimized files are stored in `codetmp/deploy`.
9298

93-
### Features Development
94-
95-
#### Google Drive Synchronization
96-
See `codetmp/js/drive.js`.
99+
**cpreview** project doesn't have minimized build, so you can publish it as is.
97100

98-
#### File Preview
99-
See `codetmp/js/preview.js`. Codetmp7 editor use communication API and workers client claiming to allow serving `codetmp` storage files requested by `cpreview` project.
100-
101-
Due to recent policy updates on browser storage partitioning, this communication only possible if both `codetmp` and `cpreview` project hosted in the same domain.
101+
### Features Development
102+
Below is the file tree that you'll most likely be working on when developing a new feature.
103+
```
104+
/codetmp
105+
/js
106+
/components -> components
107+
/uis -> components UI
108+
/require -> libraries
109+
dom-events.js -> global events mapping
110+
ui.js -> global UI
111+
view-states-map.js -> view states mapping
112+
environment.js
113+
index.js -> main app entry
114+
manifest-cache.json -> chache index
115+
```
102116

103-
#### File and Folder Management
104-
See `codetmp/js/file-manager.js`.
117+
### How Previewing Files Works
118+
Codetmp7 editor use communication API and workers client claiming to allow serving **codetmp** storage files requested by **cpreview** project.
105119

106-
#### ACE Editor
107-
See `compoEditor.Init()`. Ace editor by Ajax.org Cloud9 Editor.
120+
Due to recent policy updates on browser storage partitioning, this communication is only possible if both **codetmp** and **cpreview** project hosted in the same domain.
108121

109122

110123
## Aknowledgements

codetmp/environment.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
let environment = {
2+
activeEnvironment: 'production', // production or development
3+
4+
production: {
5+
previewUrl: 'https://preview.codetmp7.dev/',
6+
previewUrlPWA: 'https://pwa.codetmp7.dev/',
7+
},
8+
development: {
9+
previewUrl: 'http://localhost:8001/',
10+
previewUrlPWA: 'http://localhost:8002/',
11+
}
12+
};
13+
14+
export function GetEnv() {
15+
return environment[environment.activeEnvironment];
16+
}

codetmp/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ <h5><b>Trash <span class="w3-tiny">(Preview only)</span> <span class="w3-black w
814814
</script>
815815

816816
<script src="js/app.js"></script>
817-
<script src="index.js"></script>
817+
<script src="index.js" type="module"></script>
818818
<script src="pwa.js"></script>
819819

820820
<script type="module">

codetmp/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
const ACE_CDN_BASEPATH = '/assets/js/ace-builds@1.32.3/src-min-noconflict';
1+
import { GetEnv } from './environment.js';
2+
3+
let environment = GetEnv();
4+
let ACE_CDN_BASEPATH = '/assets/js/ace-builds/src-min-noconflict';
25
let activeWorkspace = 0;
3-
let environment = {
4-
previewUrl: 'https://preview.codetmp7.dev/',
5-
previewUrlPWA: 'https://pwa.codetmp7.dev/',
6-
};
76

8-
// URL for cpreview. development-only.
9-
/* let environment = {
10-
previewUrl: 'https://glorious-space-robot-v6jp7v6g5wxcxw5-5000.app.github.dev/',
11-
previewUrlPWA: 'https://glorious-space-robot-v6jp7v6g5wxcxw5-5000.app.github.dev/',
12-
}; */
7+
// use window while migrating script to module
8+
window.environment = environment;
9+
window.activeWorkspace = activeWorkspace;
10+
window.ACE_CDN_BASEPATH = ACE_CDN_BASEPATH;
1311

1412
app.loadFiles([
1513
{

codetmp/manifest-cache.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"ace": [
3-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/ace.js",
4-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/theme-github.js",
5-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/mode-html.js",
6-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/mode-json.js",
7-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/mode-css.js",
8-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/mode-javascript.js",
9-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/worker-html.js",
10-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/worker-json.js",
11-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/worker-css.js",
12-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/worker-javascript.js",
13-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/ext-searchbox.js",
14-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/ext-prompt.js",
15-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/ext-language_tools.js",
16-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/snippets/javascript.js",
17-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/snippets/html.js",
18-
"/assets/js/ace-builds@1.32.3/src-min-noconflict/snippets/markdown.js",
3+
"/assets/js/ace-builds/src-min-noconflict/ace.js",
4+
"/assets/js/ace-builds/src-min-noconflict/theme-github.js",
5+
"/assets/js/ace-builds/src-min-noconflict/mode-html.js",
6+
"/assets/js/ace-builds/src-min-noconflict/mode-json.js",
7+
"/assets/js/ace-builds/src-min-noconflict/mode-css.js",
8+
"/assets/js/ace-builds/src-min-noconflict/mode-javascript.js",
9+
"/assets/js/ace-builds/src-min-noconflict/worker-html.js",
10+
"/assets/js/ace-builds/src-min-noconflict/worker-json.js",
11+
"/assets/js/ace-builds/src-min-noconflict/worker-css.js",
12+
"/assets/js/ace-builds/src-min-noconflict/worker-javascript.js",
13+
"/assets/js/ace-builds/src-min-noconflict/ext-searchbox.js",
14+
"/assets/js/ace-builds/src-min-noconflict/ext-prompt.js",
15+
"/assets/js/ace-builds/src-min-noconflict/ext-language_tools.js",
16+
"/assets/js/ace-builds/src-min-noconflict/snippets/javascript.js",
17+
"/assets/js/ace-builds/src-min-noconflict/snippets/html.js",
18+
"/assets/js/ace-builds/src-min-noconflict/snippets/markdown.js",
1919

2020
"/assets/ace/theme-codetmp-markdown.js",
2121
"/assets/ace/theme-codetmp.js"

cpreview/index.html

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,17 @@
1313
Nothing to show here. Please go to <a href="https://codetmp.web.app">Codetmp</a> and write your code.
1414
</span>
1515

16-
<script> L = console.log </script>
17-
<script> window.$ = document.querySelector.bind(document); </script>
18-
1916
<!-- service worker handler -->
2017
<script>
2118

22-
let channelName = 'preview'; // default channel name
23-
try {
24-
channelName = location.hostname.split('.')[0]; // get sub domain name
25-
26-
// dev port settings
27-
if (location.hostname.includes(':')) {
28-
if (channelName.includes('5002')) {
29-
channelName = 'preview';
30-
} else {
31-
channelName = 'pwa';
32-
}
33-
}
34-
} catch (e) { }
19+
let channelName = (window.name == 'PreviewFramePWA' ? 'pwa' : 'preview');
3520
let controllerResolver = null;
3621

3722
if (window.opener !== null) {
3823
new Promise(function(resolve) {
3924
controllerResolver = resolve;
4025
}).then(function() {
41-
$('#js-txt-message').textContent = 'Initializing connection port ...';
26+
document.querySelector('#js-txt-message')?.replaceChildren('Initializing connection port ...');
4227
window.opener.postMessage({
4328
channelName,
4429
message: 'preview-frame-isReady',
@@ -62,15 +47,15 @@
6247

6348
if (sw.waiting) {
6449
sw.waiting.postMessage({message: 'skipWaiting'});
65-
L('Preview page updated. You might want to reload the page.');
50+
console.log('Preview page updated. You might want to reload the page.');
6651
return;
6752
}
6853

6954
if (sw.installing) {
7055
sw.installing.addEventListener('statechange', function() {
7156
if (sw.installing.state == 'installed') {
7257
sw.waiting.postMessage({message: 'skipWaiting'});
73-
L('Preview page updated. You might want to reload the page.');
58+
console.log('Preview page updated. You might want to reload the page.');
7459
}
7560
})
7661
return;
@@ -80,7 +65,7 @@
8065
sw.installing.addEventListener('statechange', function() {
8166
if (this.state == 'installed') {
8267
sw.waiting.postMessage({message: 'skipWaiting'});
83-
L('Preview page updated. You might want to reload the page.');
68+
console.log('Preview page updated. You might want to reload the page.');
8469
}
8570
})
8671
})
@@ -102,11 +87,13 @@
10287
switch (e.data.message) {
10388
case 'init-message-port':
10489
navigator.serviceWorker.controller.postMessage({
90+
channelName,
10591
message: 'init-message-port',
10692
}, [e.ports[0]]);
10793
break;
10894
case 'reinit-message-port':
10995
navigator.serviceWorker.controller.postMessage({
96+
channelName,
11097
message: 'reinit-message-port',
11198
}, [e.ports[0]]);
11299
break;

cpreview/sw.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
let asd = L = console.log;
2-
let cacheVersion = '10';
1+
let cacheVersion = '11';
32
let cacheItem = 'cpreview-'+cacheVersion;
43
let messagePort;
54
let resolverQueue = {};
@@ -8,17 +7,6 @@ let catchedBlob = {};
87
let isRelinkingMessagePort = false;
98

109
let channelName = 'preview';
11-
try {
12-
channelName = location.hostname.split('.')[0]; // get sub domain name
13-
// dev port settings
14-
if (location.hostname.includes(':')) {
15-
if (channelName.includes('5002')) {
16-
channelName = 'preview';
17-
} else {
18-
channelName = 'pwa';
19-
}
20-
}
21-
} catch (e) { }
2210

2311
function portMessageHandler(e) {
2412
switch (e.data.message) {
@@ -80,11 +68,13 @@ self.addEventListener('message', async function(e) {
8068
self.skipWaiting();
8169
break;
8270
case 'reinit-message-port':
71+
channelName = e.data.channelName;
8372
messagePort = e.ports[0];
8473
messagePort.onmessage = portMessageHandler;
8574
isRelinkingMessagePort = false;
8675
break;
8776
case 'init-message-port':
77+
channelName = e.data.channelName;
8878
messagePort = e.ports[0];
8979
messagePort.onmessage = portMessageHandler;
9080
messagePort.postMessage({ message: 'message-port-opened' });
@@ -165,7 +155,7 @@ function relinkMissingPort() {
165155
return relinkingPromise;
166156
}
167157

168-
relinkingPromise =new Promise((resolve, reject) => {
158+
relinkingPromise = new Promise((resolve, reject) => {
169159

170160
self.clients.matchAll({
171161
includeUncontrolled: true,
@@ -233,7 +223,7 @@ function responseByFetch(e, resolve) {
233223
self.addEventListener('fetch', function(e) {
234224

235225
let reqUrl = new URL(e.request.url);
236-
if (reqUrl.pathName == '/') {
226+
if (reqUrl.pathname == '/') {
237227
e.respondWith(
238228
caches.match(e.request).then(function(resp) {
239229
if (resp) {

0 commit comments

Comments
 (0)