feat: Initial UI files

This commit is contained in:
Luke authored and Luke committed 2021-12-26 15:41:43 +01:00
1 parent 4498dddbac
commit 5bbf30e4b4
17 files changed
+398

No files matched your search

+7
View File
@@ -7,6 +7,8 @@ version '1.9.0'
url 'https://github.com/overextended/oxmysql'
author 'overextended'
ui_page 'src/ui/public/index.html'
dependencies {
'/server:5104',
}
@@ -15,4 +17,9 @@ server_scripts {
'dist/server/build.js',
}
files {
'src/ui/public/index.html',
'src/ui/public/**/*'
}
provide 'mysql-async'
+4
View File
@@ -0,0 +1,4 @@
/node_modules/
/public/build/
.DS_Store
+29
View File
@@ -0,0 +1,29 @@
{
"name": "svelte-app",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public --no-clear"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"@rollup/plugin-typescript": "^8.0.0",
"@tsconfig/svelte": "^2.0.0",
"rollup": "^2.3.4",
"rollup-plugin-css-only": "^3.1.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^7.0.0",
"rollup-plugin-terser": "^7.0.0",
"svelte": "^3.0.0",
"svelte-check": "^2.0.0",
"svelte-preprocess": "^4.0.0",
"tslib": "^2.0.0",
"typescript": "^4.0.0"
},
"dependencies": {
"sirv-cli": "^1.0.0"
}
}
+8
View File
@@ -0,0 +1,8 @@
* {
margin: 0;
padding: 0;
}
body {
height: 100vh;
}
+16
View File
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Svelte app</title>
<link rel="stylesheet" href="./global.css" />
<link rel="stylesheet" href="./build/bundle.css" />
<script defer src="./build/bundle.js"></script>
</head>
<body></body>
</html>
+87
View File
@@ -0,0 +1,87 @@
import svelte from "rollup-plugin-svelte";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import livereload from "rollup-plugin-livereload";
import { terser } from "rollup-plugin-terser";
import sveltePreprocess from "svelte-preprocess";
import typescript from "@rollup/plugin-typescript";
import css from "rollup-plugin-css-only";
const production = !process.env.ROLLUP_WATCH;
function serve() {
let server;
function toExit() {
if (server) server.kill(0);
}
return {
writeBundle() {
if (server) return;
server = require("child_process").spawn(
"npm",
["run", "start", "--", "--dev"],
{
stdio: ["ignore", "inherit", "inherit"],
shell: true,
}
);
process.on("SIGTERM", toExit);
process.on("exit", toExit);
},
};
}
export default {
input: "src/main.ts",
output: {
sourcemap: false,
format: "iife",
name: "app",
file: "public/build/bundle.js",
},
plugins: [
svelte({
preprocess: sveltePreprocess({ sourceMap: !production }),
compilerOptions: {
// enable run-time checks when not in production
dev: !production,
},
}),
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: "bundle.css" }),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ["svelte"],
}),
commonjs(),
typescript({
sourceMap: !production,
inlineSources: !production,
}),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload("public"),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser(),
],
watch: {
clearScreen: false,
},
};
+18
View File
@@ -0,0 +1,18 @@
<script lang="ts">
import VisibilityProvider from './providers/VisibilityProvider.svelte';
import HelloWorld from './components/HelloWorld.svelte';
import { debugData } from './utils/debugData';
debugData([
{
action: 'setVisible',
data: true,
},
]);
</script>
<main>
<VisibilityProvider>
<HelloWorld />
</VisibilityProvider>
</main>
+81
View File
@@ -0,0 +1,81 @@
<script lang="ts">
import { visibility } from '../store/stores';
import { fetchNui } from '../utils/fetchNui';
interface ReturnData {
x: number;
y: number;
z: number;
}
let clientData: ReturnData;
const handleClientData = () => {
fetchNui('getClientData')
.then((returnData) => {
clientData = returnData;
})
.catch((e) => {
clientData = { x: 100, y: 100, z: 100 };
});
};
const closeDialog = () => {
visibility.set(false);
fetchNui('hideUI');
};
</script>
<div class="container">
<h2>Svelte NUI Popup!</h2>
<div style="margin: 0.3vh 0;">Player coords: {JSON.stringify(clientData) || ''}</div>
<div class="btn-wrapper">
<button on:click={handleClientData}> Get client coords </button>
<button on:click={closeDialog}>Close popup</button>
</div>
<span>Or press the escape key!</span>
</div>
<style>
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
align-content: center;
justify-content: center;
border-radius: 5px;
width: 17%;
height: 17%;
font-size: 1.5vh;
color: white;
background-color: #ff3c04;
user-select: none;
}
.btn-wrapper {
display: flex;
justify-content: space-evenly;
align-items: center;
align-content: center;
width: 100%;
}
button {
flex: 0 45%;
padding: 0.8vh;
background-color: white;
color: black;
font-family: 'Segoe UI';
border: none;
border-radius: 5px;
}
button:hover {
background-color: rgb(212, 212, 212);
}
</style>
+1
View File
@@ -0,0 +1 @@
/// <reference types="svelte" />
+7
View File
@@ -0,0 +1,7 @@
import App from "./App.svelte";
const app = new App({
target: document.body,
});
export default app;
@@ -0,0 +1,35 @@
<script lang="ts">
import { useNuiEvent } from '../utils/useNuiEvent';
import { fetchNui } from '../utils/fetchNui';
import { onMount } from 'svelte';
import { visibility } from '../store/stores';
let isVisible: boolean;
visibility.subscribe((visible) => {
isVisible = visible;
});
useNuiEvent<boolean>('setVisible', (visible) => {
visibility.set(visible);
});
onMount(() => {
const keyHandler = (e: KeyboardEvent) => {
if (isVisible && ['Escape'].includes(e.code)) {
fetchNui('hideUI');
visibility.set(false);
}
};
window.addEventListener('keydown', keyHandler);
return () => window.removeEventListener('keydown', keyHandler);
});
</script>
<main>
{#if isVisible}
<slot />
{/if}
</main>
+3
View File
@@ -0,0 +1,3 @@
import { writable } from "svelte/store";
export const visibility = writable(false);
+30
View File
@@ -0,0 +1,30 @@
import {isEnvBrowser} from "./misc";
interface DebugEvent<T = any> {
action: string;
data: T;
}
/**
* Emulates dispatching an event using SendNuiMessage in the lua scripts.
* This is used when developing in browser
*
* @param events - The event you want to cover
* @param timer - How long until it should trigger (ms)
*/
export const debugData = <P>(events: DebugEvent<P>[], timer = 1000): void => {
if (isEnvBrowser()) {
for (const event of events) {
setTimeout(() => {
window.dispatchEvent(
new MessageEvent("message", {
data: {
action: event.action,
data: event.data,
},
})
);
}, timer);
}
}
};
+27
View File
@@ -0,0 +1,27 @@
/**
* @param eventName - The endpoint eventname to target
* @param data - Data you wish to send in the NUI Callback
*
* @return returnData - A promise for the data sent back by the NuiCallbacks CB argument
*/
export async function fetchNui<T = any>(
eventName: string,
data: unknown = {}
): Promise<T> {
const options = {
method: "post",
headers: {
"Content-Type": "application/json; charset=UTF-8",
},
body: JSON.stringify(data),
};
const resourceName = (window as any).GetParentResourceName
? (window as any).GetParentResourceName()
: "nui-frame-app";
const resp = await fetch(`https://${resourceName}/${eventName}`, options);
return await resp.json();
}
+1
View File
@@ -0,0 +1 @@
export const isEnvBrowser = (): boolean => !(window as any).invokeNative;
+31
View File
@@ -0,0 +1,31 @@
import { onMount, onDestroy } from "svelte";
interface NuiMessage<T = unknown> {
action: string;
data: T;
}
/**
* A function that manage events listeners for receiving data from the client scripts
* @param action The specific `action` that should be listened for.
* @param handler The callback function that will handle data relayed by this function
*
* @example
* useNuiEvent<{visibility: true, wasVisible: 'something'}>('setVisible', (data) => {
* // whatever logic you want
* })
*
**/
export function useNuiEvent<T = unknown>(
action: string,
handler: (data: T) => void
) {
const eventListener = (event: MessageEvent<NuiMessage<T>>) => {
const { action: eventAction, data } = event.data;
eventAction === action && handler(data);
};
onMount(() => window.addEventListener("message", eventListener));
onDestroy(() => window.removeEventListener("message", eventListener));
}
+13
View File
@@ -0,0 +1,13 @@
{
"extends": "@tsconfig/svelte/tsconfig.json",
"compilerOptions": {
"strict": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*"],
"exclude": ["node_modules/*", "__sapper__/*", "public/*"]
}