summaryrefslogtreecommitdiff
path: root/plugins/vue/plugin.js
diff options
context:
space:
mode:
authorsportdeath2023-02-01 20:01:31 -0500
committerGitHub2023-02-01 20:01:31 -0500
commit67eca331d5cdbd9df5102b4463567f948faefdfd (patch)
tree473b7922b8ed1a549a41c41393ee2b7ac8137ece /plugins/vue/plugin.js
parentb4a69044a200e1d06f86f21489a3470a90d835cf (diff)
parenta2cc33b1de416bef592ecf25816f17017e62fe8e (diff)
Merge pull request #1 from graffiti-garden/tags
Tags
Diffstat (limited to 'plugins/vue/plugin.js')
-rw-r--r--plugins/vue/plugin.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/plugins/vue/plugin.js b/plugins/vue/plugin.js
new file mode 100644
index 0000000..2c61f40
--- /dev/null
+++ b/plugins/vue/plugin.js
@@ -0,0 +1,58 @@
+import { ref, reactive } from 'vue'
+import Graffiti from '../../graffiti.js'
+
+export default {
+ install(app, options) {
+
+ const graffitiURL = options && 'url' in options?
+ options.url : 'https://graffiti.garden'
+
+ // Initialize graffiti
+ const graffiti = new Graffiti(graffitiURL, ()=>reactive({}))
+
+ // These ID need to change after opening
+ app.config.globalProperties.$graffitiID = ref(null)
+ graffiti.opened().then(()=> {
+ app.config.globalProperties.$graffitiID.value = graffiti.myID
+ })
+
+ // Add logging in and out
+ app.config.globalProperties.$graffitiToggleLogIn =
+ graffiti.toggleLogIn.bind(graffiti)
+
+ // A composable for subscribing and
+ // unsubscribing to tags that returns
+ // a reactive array of the results
+ app.component('GraffitiObjects', {
+
+ props: ['tags'],
+
+ watch: {
+ tags: {
+ async handler(newTags, oldTags=[]) {
+ // Subscribe to the new tags
+ await graffiti.subscribe(...newTags)
+ // Unsubscribe to the existing tags
+ await graffiti.unsubscribe(...oldTags)
+ },
+ immediate: true,
+ deep: true
+ }
+ },
+
+ // Handle unmounting too
+ unmount() {
+ graffiti.unsubscribe(this.tags)
+ },
+
+ computed: {
+ objects() {
+ return graffiti.objectsByTags(...this.tags)
+ }
+ },
+
+ template: '<slot :objects="objects"></slot>'
+ })
+
+ }
+}