Skip to content

Commit

Permalink
Merge pull request #10 from mohamadreza1388/main
Browse files Browse the repository at this point in the history
editor
  • Loading branch information
BaseMax authored Sep 16, 2024
2 parents 58b72ed + 35aeb03 commit 59b7c1c
Show file tree
Hide file tree
Showing 9 changed files with 5,004 additions and 1 deletion.
Binary file added public/assets/images/favicon.ico
Binary file not shown.
19 changes: 19 additions & 0 deletions public/assets/salam/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "زبان برنامه نویسی سلام",
"short_name": "زبان سلام",
"icons": [
{
"src": "/image/android-chrome-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/image/android-chrome-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"theme_color": "#ff5c00",
"background_color": "#ffffff",
"display": "standalone"
}
4,659 changes: 4,659 additions & 0 deletions public/assets/salam/salam-wa.js

Large diffs are not rendered by default.

Binary file added public/assets/salam/salam-wa.wasm
Binary file not shown.
185 changes: 185 additions & 0 deletions public/assets/salam/script/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Elements
const elm_code = document.querySelector('.code');
const elm_output = document.querySelector('.output');
const elm_error = document.querySelector('.error');
const elm_iframe = document.querySelector('iframe');
const elm_run_code = document.querySelector('.run_code');

// Global variables
var Module = {
noInitialRun: true,
onRuntimeInitialized: () => {
console.log('Salam loaded successfully');

if (elm_code.value !== '') {
runSalam(false);
}
},
print: (text) => {
displayOutput(text);
},
printErr: (text) => {
displayError(text);
},
};

// Functions
const displayOutput = (text) => {
console.log("Output: ", text);
elm_output.textContent += text + '\n';
};

const displayError = (text) => {
console.error("Error: ", text);

// TODO: Ignore keepRuntimeAlive() warning
if (text === "program exited (with status: 2), but keepRuntimeAlive() is set (counter=0) due to an async operation, so halting execution but not exiting the runtime or preventing further async execution (you can use emscripten_force_exit, if you want to force a true shutdown)") {
return;
}

elm_error.textContent += text + '<br>';
};

const getIframeContent = (iframe) => {
return iframe.contentDocument || iframe.contentWindow.document;
};

const showErrorInIframe = () => {
const iframeDocument = getIframeContent(elm_iframe);

if (iframeDocument) {
iframeDocument.open();
iframeDocument.write(`<!DOCTYPE html>
<html dir="rtl" lang="fa-IR">
<body>
<div style="color: red; font-weight: bold;"><b>خطا:</b> ${elm_error.innerHTML}</div>
</body>
</html>`);
iframeDocument.close();
}
};

const captureLint = (arguments) => {
console.log("Capture Lint: ", arguments);

elm_output.textContent = '';
elm_error.textContent = '';

try {
const exitCode = callMain(arguments);

if (exitCode !== 0) {
return null;
} else {
return elm_output.textContent;
}
} catch (err) {
return null;
}
};

const captureOutput = (showOutput, arguments) => {
console.log("Capture Output: ", arguments);

elm_output.textContent = '';
elm_error.textContent = '';

try {
const exitCode = callMain(arguments);

if (exitCode !== 0) {
elm_error.innerHTML = 'برنامه با خطا مواجه شد.<br>' + elm_error.textContent;
showErrorInIframe();
} else {
const iframeDocument = getIframeContent(elm_iframe);

if (iframeDocument) {
iframeDocument.open();
iframeDocument.write(elm_output.textContent);
iframeDocument.close();
}
}
} catch (err) {
console.error(err);

elm_error.textContent = 'خطای غیرمنتظره رخ داد.';
showErrorInIframe();
}
};

const runLint = () => {
console.log('Running Salam lint...');

if (!isReady) {
console.log('Salam runtime not ready. Please wait...');
return;
}

const code = elm_code.value.toString().trim();
if (!code) {
return;
}

const arguments = ['lint', 'code', code];

const res = captureLint(arguments);
if (res !== null) {
elm_code.value = res.toString().trim();
}
};

const runSalam = (showOutput) => {
console.log('Running Salam code...');

const code = elm_code.value.toString().trim();
if (!code) {
elm_error.innerHTML = '';
elm_output.innerHTML = '';

return;
}

const arguments = ['code', code];

captureOutput(showOutput, arguments);
};

// Events
elm_code.addEventListener('keydown', (event) => {
if (event.key === 'Tab') {
event.preventDefault();

const textarea = event.target;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;

textarea.value = textarea.value.substring(0, start) + '\t' + textarea.value.substring(end);
textarea.selectionStart = textarea.selectionEnd = start + 1;
}
});

elm_code.addEventListener("input", () => {
runSalam(false);
});
elm_run_code.addEventListener("click", () => {
runSalam(false)
})
// Init
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/assets/salam/salam-wa.js';
document.body.appendChild(script);

window.addEventListener('load', () => {
elm_code.focus();
});

// Cache
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/assets/salam/script/service-worker.js').then(() => {
console.log('Service Worker Registered');
})
.catch(error => {
console.log('Service Worker Registration Failed:', error);
});
}
76 changes: 76 additions & 0 deletions public/assets/salam/script/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const CACHE_NAME = 'salam-cache-v1';

const ASSETS_TO_CACHE = [
'/site.webmanifest',

// style
'/style/style.css',

// script
'/script/script.js',

// salam
'/salam-wa.js',
'/salam-wa.wasm',

// images
'/image/android-chrome-192x192.png',
'/image/android-chrome-512x512.png',
'/image/apple-touch-icon.png',
'/image/favicon-16x16.png',
'/image/favicon-32x32.png',
'/image/favicon.ico',

'/image/theme-light.jpg',
'/image/theme-dark.jpg',

'/image/view-full.jpg',
'/image/view-split.jpg',

'/image/image/screenshot.jpg',
];

// Install Event - Cache the defined assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(ASSETS_TO_CACHE);
})
.then(() => self.skipWaiting())
);
});

// Activate Event - Clean up old caches
self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (!cacheWhitelist.includes(cacheName)) {
return caches.delete(cacheName);
}
})
);
}).then(() => self.clients.claim())
);
});

// Fetch Event - Serve cached assets, update cache when online
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(cachedResponse => {
const fetchPromise = fetch(event.request).then(networkResponse => {
if (networkResponse && networkResponse.status === 200) {
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, networkResponse.clone());
});
}
return networkResponse;
}).catch(() => cachedResponse);

return cachedResponse || fetchPromise;
})
);
});
3 changes: 3 additions & 0 deletions resources/css/editor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
62 changes: 61 additions & 1 deletion resources/views/editor.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,68 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Salam Editor</title>
<link rel="shortcut icon" href="{{ asset("assets/images/favicon.ico") }}" type="image/x-icon">
@vite(["resources/css/editor.css", "resources/css/app.css"])
<style>
@font-face {
font-family: "estedad";
font-weight: 100;
src: url({{ asset("assets/fonts/Estedad-Thin.ttf") }});
}
@font-face {
font-family: "estedad";
font-weight: 300;
src: url({{ asset("assets/fonts/Estedad-Light.ttf") }});
}
@font-face {
font-family: "estedad";
font-weight: 400;
src: url({{ asset("assets/fonts/Estedad-Medium.ttf") }});
}
@font-face {
font-family: "estedad";
font-weight: 700;
src: url({{ asset("assets/fonts/Estedad-Bold.ttf") }});
}
@font-face {
font-family: "estedad";
font-weight: 900;
src: url({{ asset("assets/fonts/Estedad-Black.ttf") }});
}
* {
font-family: "estedad", sans-serif !important;
}
</style>
</head>
<body>
<body class="h-[100vh]">

<div class="my_container w-[98%] mx-auto flex justify-content-center gap-4 py-4 h-full md:flex-row flex-col">
<div class="basis-1/2 h-full flex flex-col">
<header class="bg-[#ff620021] basis-[65px] px-[10px] w-full rounded-[20px] flex justify-between items-center">
<button class="run_code h-[50px] active:opacity-80 px-2 rounded-[15px] bg-[#FF6100] flex justify-center items-center gap-2 text-white">
<span class="title block">اجرا</span>
<svg viewBox="0 0 24 24" class="w-[21px]" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="SVGRepo_bgCarrier" stroke-width="0"></g>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round" stroke="#000000" stroke-width="0.048"></g>
<g id="SVGRepo_iconCarrier">
<path d="M3 12L3 18.9671C3 21.2763 5.53435 22.736 7.59662 21.6145L10.7996 19.8727M3 8L3 5.0329C3 2.72368 5.53435 1.26402 7.59661 2.38548L20.4086 9.35258C22.5305 10.5065 22.5305 13.4935 20.4086 14.6474L14.0026 18.131" stroke="#ffffff" stroke-width="2.4" stroke-linecap="round"></path>
</g>
</svg>
</button>
</header>
<textarea class="code bg-[#ff620021] flex-1 w-full rounded-[20px] text-[25px] text-black mt-4 p-[15px] transition-all duration-300 !outline-0 !border-0 resize-none"></textarea>
</div>
<div class="basis-1/2">
<iframe class="w-full h-full border rounded-[20px] border-1 border-[#FF6100] shadow-[0_0_20px_-10px_#FF6100]"></iframe>
<pre class="error !hidden"></pre>
<pre class="output !hidden"></pre>
</div>
</div>
<script src="{{ asset("assets/salam/script/script.js") }}"></script>
</body>
</html>
1 change: 1 addition & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
'resources/css/app.css',
'resources/sass/app.scss',
'resources/js/app.js',
'resources/css/editor.css',
],
refresh: true,
}),
Expand Down

0 comments on commit 59b7c1c

Please sign in to comment.