Skip to content

Commit ae25090

Browse files
committed
setup editor
1 parent 2b0c2a8 commit ae25090

File tree

6 files changed

+172
-33
lines changed

6 files changed

+172
-33
lines changed

src/App.vue

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,22 @@
11
<template>
2-
<div>
3-
hello world
4-
</div>
5-
<button @click="login">login</button>
6-
<button @click="listQuery">list</button>
2+
<LoginScreen v-if="!accessToken" v-model:access-token="accessToken"/>
3+
<WorkScreen v-else></WorkScreen>
74
</template>
85

96
<script setup lang="ts">
10-
import {ref, computed} from 'vue';
11-
import {auth} from './auth';
12-
import listGql from './List.gql';
13-
import { graphql } from '@octokit/graphql';
7+
import {ref, watch} from 'vue';
8+
import {setAccessToken} from "@/useGraphQlClient";
149
15-
const accessToken = ref<string | null>(null);
10+
export {default as LoginScreen} from './LoginScreen.vue';
11+
export {default as WorkScreen} from './WorkScreen.vue';
1612
17-
const graphQlClient = computed(() => {
18-
const headers = accessToken.value ? {authorization: `token ${accessToken.value}`}: {};
19-
alert(headers);
20-
return graphql.defaults({
21-
headers
22-
});
23-
});
24-
25-
export const login = async () => {
26-
const {access_token} = await auth();
27-
accessToken.value = access_token;
28-
alert('logged in');
29-
};
30-
31-
export const listQuery = async () => {
32-
if(!graphQlClient.value){
33-
return;
34-
}
35-
const result = await graphQlClient.value(listGql);
36-
alert(JSON.stringify(result, null, 2));
37-
};
13+
export const accessToken = ref<string | null>(null);
14+
watch(() => accessToken.value, (accessToken) => setAccessToken(accessToken));
3815
3916
</script>
4017

41-
<style scoped>
42-
#app {
18+
<style>
19+
body {
4320
font-family: Avenir, Helvetica, Arial, sans-serif;
4421
-webkit-font-smoothing: antialiased;
4522
-moz-osx-font-smoothing: grayscale;

src/AutoLogin.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<label>Auto login</label>
3+
<input
4+
v-model="autoLogin"
5+
type="checkbox"
6+
class="form-checkbox"
7+
/>
8+
</template>
9+
10+
<script setup lang="ts">
11+
import {ref, watch} from "vue";
12+
13+
export const autoLogin = ref<string | null>( window.localStorage.getItem('autoLogin'));
14+
15+
watch(() => autoLogin.value, (autoLogin) => {
16+
if(autoLogin){
17+
window.localStorage.setItem('autoLogin', autoLogin.toString());
18+
} else {
19+
window.localStorage.removeItem('autoLogin');
20+
}
21+
});
22+
</script>

src/LoginScreen.vue

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
<div>
3+
<button @click="login">login</button>
4+
<div>
5+
You need to login to github, to use your gists with this dev tool.
6+
</div>
7+
<AutoLogin/>
8+
9+
</div>
10+
</template>
11+
12+
<script setup="props, {emit}" lang="ts">
13+
export {default as AutoLogin} from './AutoLogin.vue';
14+
import {onMounted} from "vue";
15+
import {auth} from "@/auth";
16+
17+
export const login = async () => {
18+
const {access_token} = await auth();
19+
emit('update:access-token', access_token);
20+
};
21+
22+
onMounted(()=> {
23+
if(window.localStorage.getItem('autoLogin') !== null){
24+
login();
25+
}
26+
});
27+
28+
29+
</script>

src/MonacoEditor.vue

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<template>
2+
<div ref="div"/>
3+
</template>
4+
5+
<script setup="props, { emit }" lang="ts">
6+
import {onMounted, ref, watch} from "vue";
7+
import * as monaco from 'monaco-editor';
8+
9+
declare const props: {
10+
code: string;
11+
};
12+
13+
export const div = ref<HTMLDivElement | null>(null);
14+
15+
onMounted(() => {
16+
if (!div.value) {
17+
return;
18+
}
19+
20+
const editor = monaco.editor.create(div.value, {
21+
value: props.code,
22+
language: 'javascript',
23+
scrollBeyondLastLine: false,
24+
automaticLayout: true
25+
});
26+
27+
watch(() => props.code, () => {
28+
if (editor.getValue() !== props.code) {
29+
editor.setValue(props.code);
30+
}
31+
});
32+
editor.onDidChangeModelContent(() => {
33+
emit('update:code', editor.getValue() ?? '');
34+
});
35+
});
36+
</script>

src/WorkScreen.vue

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<template>
2+
<div class="grid app-grid-template h-screen">
3+
<div style="grid-area: head; background-color: red">
4+
<AutoLogin/>
5+
</div>
6+
<div style="grid-area: nav; background-color: green">
7+
<button @click="listQuery">refresh</button>
8+
<a v-for="entry in gistEntries" :key="entry.id" href="javascript:void(0)" @click="code = entry.code">
9+
{{ entry.name }}
10+
</a>
11+
</div>
12+
<div style="grid-area: main; background-color: yellow">
13+
<MonacoEditor :code="code" style="height: calc(100% - 50px)"/>
14+
</div>
15+
</div>
16+
</template>
17+
18+
<script setup lang="ts">
19+
import {List} from "@/__gen_gql/List";
20+
import listGql from "@/List.gql";
21+
import {useGraphQlClient} from "@/useGraphQlClient";
22+
import {onMounted, ref} from "vue";
23+
export {default as AutoLogin} from './AutoLogin';
24+
export {default as MonacoEditor} from './MonacoEditor';
25+
26+
const graphQlClient = useGraphQlClient();
27+
28+
interface GistEntries {
29+
id: string;
30+
name: string;
31+
code: string;
32+
}
33+
34+
export const gistEntries = ref<GistEntries[]>([]);
35+
export const listQuery = async () => {
36+
gistEntries.value = (await graphQlClient.value<List>(listGql)).viewer.gists.edges.map(({node}) => ({
37+
id: node.name,
38+
name: node.files[0].name,
39+
code: node.files[0].text
40+
}));
41+
};
42+
43+
export const code = ref<string>("");
44+
45+
onMounted(() => {
46+
listQuery();
47+
});
48+
49+
</script>
50+
51+
<style scoped>
52+
.app-grid-template {
53+
grid-template-areas:
54+
"head head"
55+
"nav main";
56+
grid-template-rows: 70px 1fr;
57+
grid-template-columns: 150px 1fr;
58+
}
59+
</style>
60+

src/useGraphQlClient.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {graphql} from "@octokit/graphql";
2+
import {computed, ref, Ref} from "vue";
3+
4+
const _accessToken = ref<string | null>(null);
5+
export const setAccessToken = (accessToken: string | null) => {
6+
_accessToken.value = accessToken;
7+
};
8+
9+
export const useGraphQlClient = (): Ref<typeof graphql> =>
10+
computed(() => {
11+
const headers = _accessToken.value ? {authorization: `token ${_accessToken.value}`} : {};
12+
return graphql.defaults({
13+
headers
14+
});
15+
});

0 commit comments

Comments
 (0)