@code-coaching/vuetiful 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-coaching/vuetiful",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "scripts": {
5
5
  "dev": "onchange 'src/**/*.vue' 'src/**/*.ts' 'src/**/*.css' -- npm run build",
6
6
  "prebuild": "node 'scripts/intellisense.js'",
@@ -13,6 +13,9 @@
13
13
  "peerDependencies": {
14
14
  "vue": "^3.2.25"
15
15
  },
16
+ "dependencies": {
17
+ "highlight.js": "^11.7.0"
18
+ },
16
19
  "devDependencies": {
17
20
  "@types/node": "^17.0.14",
18
21
  "@vitejs/plugin-vue": "^2.0.0",
@@ -0,0 +1,9 @@
1
+ import { DirectiveBinding } from "vue";
2
+
3
+ const clipboard = (el: HTMLElement, binding: DirectiveBinding) => {
4
+ el.addEventListener("click", () => {
5
+ navigator.clipboard.writeText(binding.value);
6
+ });
7
+ };
8
+
9
+ export default clipboard;
@@ -0,0 +1,5 @@
1
+ import vClipboard from './clipboard'
2
+
3
+ export {
4
+ vClipboard
5
+ }
package/src/index.ts CHANGED
@@ -10,20 +10,12 @@ function install(app: any) {
10
10
  import "./assets/main.css";
11
11
  import "./styles/tailwind.css";
12
12
 
13
- import "./themes/theme-vuetiful.css";
14
- // import "./themes/theme-modern.css";
15
- // import "./themes/theme-rocket.css";
16
- // import "./themes/theme-sahara.css";
17
- // import "./themes/theme-seafoam.css";
18
- // import "./themes/theme-seasonal.css";
19
- // import "./themes/theme-test.css";
20
- // import "./themes/theme-vintage.css";
21
-
22
13
  export default { install };
23
14
 
24
15
  export * from "./components";
25
16
  export * from "./constants";
26
17
  export * from "./utils";
18
+ export * from "./directives";
27
19
 
28
20
  // This type alias is to identify CSS classes within component props, which enables Tailwind IntelliSense
29
21
  export type CssClasses = string;
@@ -0,0 +1,106 @@
1
+ <script setup lang="ts">
2
+ import { CssClasses, vClipboard } from "@/index";
3
+ import "highlight.js/styles/github-dark.css";
4
+ import { computed, ref, useAttrs } from "vue";
5
+ import { useHighlight } from "./highlight";
6
+
7
+ const { highlight } = useHighlight();
8
+
9
+ const props = defineProps({
10
+ language: {
11
+ type: String,
12
+ default: "plaintext",
13
+ },
14
+ code: {
15
+ type: String,
16
+ default: "",
17
+ },
18
+ background: {
19
+ type: String as () => CssClasses,
20
+ default: "bg-neutral-900/90",
21
+ },
22
+ blur: {
23
+ type: String as () => CssClasses,
24
+ default: "",
25
+ },
26
+ text: {
27
+ type: String as () => CssClasses,
28
+ default: "text-sm",
29
+ },
30
+ color: {
31
+ type: String as () => CssClasses,
32
+ default: "text-white",
33
+ },
34
+ rounded: {
35
+ type: String as () => CssClasses,
36
+ default: "rounded-container-token",
37
+ },
38
+ shadow: {
39
+ type: String as () => CssClasses,
40
+ default: "shadow",
41
+ },
42
+
43
+ button: {
44
+ type: String as () => CssClasses,
45
+ default: "btn btn-sm variant-soft",
46
+ },
47
+ buttonLabel: {
48
+ type: String,
49
+ default: "Copy",
50
+ },
51
+ buttonCopied: {
52
+ type: String,
53
+ default: "👍",
54
+ },
55
+ });
56
+
57
+ const attrs = useAttrs();
58
+ const emit = defineEmits<{
59
+ (event: "copy"): void;
60
+ }>();
61
+
62
+ const cBase = "overflow-hidden shadow";
63
+ const cHeader = "text-xs text-white/50 uppercase flex justify-between items-center p-2 pl-4 pb-0";
64
+ const cPre = "whitespace-pre-wrap break-all p-4 pt-1";
65
+
66
+ const copyState = ref(false);
67
+
68
+ // Allow shorthand alias, but show full text in UI
69
+ function languageFormatter(lang: string): string {
70
+ if (lang === "js") return "javascript";
71
+ if (lang === "ts") return "typescript";
72
+ if (["sh", "bash", "zsh", "shell"].includes(lang)) return "console";
73
+ return lang;
74
+ }
75
+
76
+ function onCopyClick() {
77
+ copyState.value = true;
78
+ setTimeout(() => {
79
+ copyState.value = false;
80
+ }, 2000);
81
+ emit("copy");
82
+ }
83
+
84
+ // Reactive
85
+ const classesBase = computed(
86
+ () =>
87
+ `${cBase} ${props.background} ${props.blur} ${props.text} ${props.color} ${props.rounded} ${
88
+ props.shadow
89
+ } ${attrs.class ?? ""}`
90
+ );
91
+ </script>
92
+
93
+ <!-- prettier-ignore -->
94
+ <template>
95
+ <div v-if="language && code">
96
+ <div :class="`code-block ${classesBase}`" data-testid="code-block">
97
+ <header :class="`code-block-header ${cHeader}`">
98
+ <span :class="`code-block-language`">{{languageFormatter(language)}}</span>
99
+ <button :class="`code-block-btn ${button}`" @click="onCopyClick()" v-clipboard="props.code">
100
+ {{!copyState ? buttonLabel : buttonCopied}}
101
+ </button>
102
+ </header>
103
+ <pre :class="`code-block-pre ${cPre}`"><code :class="`code-block-code language-${language}`" v-html="highlight(props.code, props.language)"></code></pre>
104
+ </div>
105
+ </div>
106
+ </template>
@@ -0,0 +1,19 @@
1
+ import hljs from "highlight.js";
2
+
3
+ const highlighter = hljs;
4
+
5
+ const useHighlight = () => {
6
+ const highlight = (code: string, lang: string) => {
7
+ if (lang && highlighter.getLanguage(lang)) {
8
+ return highlighter.highlight(lang, code).value;
9
+ } else {
10
+ return highlighter.highlightAuto(code).value;
11
+ }
12
+ };
13
+
14
+ return {
15
+ highlight,
16
+ };
17
+ };
18
+
19
+ export { useHighlight };
@@ -1,7 +1,8 @@
1
1
  import { useDarkMode } from "./dark-mode/dark-mode";
2
2
  import DarkModeSwitch from "./dark-mode/dark-mode.vue";
3
- import MyUtil from "./MyUtil";
4
3
  import { useTheme } from "./theme/theme";
5
4
  import ThemeSwitcher from "./theme/theme-switcher.vue";
6
5
 
7
- export { MyUtil, DarkModeSwitch, ThemeSwitcher, useDarkMode, useTheme };
6
+ import CodeBlock from "./code-block/code-block.vue";
7
+
8
+ export { DarkModeSwitch, ThemeSwitcher, useDarkMode, useTheme, CodeBlock };
@@ -1,5 +0,0 @@
1
- declare function add(a: number, b: number): number;
2
- declare const _default: {
3
- add: typeof add;
4
- };
5
- export default _default;
@@ -1,7 +0,0 @@
1
- function add(a: number, b: number): number {
2
- return a + b;
3
- }
4
-
5
- export default {
6
- add,
7
- };