Assume you've installed Annotative Code already
- Import
annotative-code,languageFnandtheme
Import annotative-code to make the web component available to the Vue app.
Import only the relevant languageFn and themeCss to minimize the bundle size.
Here is the full list of programming languages (opens in a new tab)
and themes (opens in a new tab) brought to you by
highlight.js (opens in a new tab)
/components/CodeDemo.vue
<script setup>
import 'annotative-code';
import languageFn from 'annotative-code/bin/highlight.js/languages/javascript';
import themeCss from 'annotative-code/bin/highlight.js/css/a11y-dark';
</script>- Define the
contentandannotation
contentis the code/content string we pass to highlight.js- Inside the
contentstring, you can enclose certain text with ____ (4 underscore), it will become a placeholder - Annotative Code will insert an annotation popup to placeholders based on the
annotationobject - In the following example, we define a
contentstring with a placeholder____severity____inside it. If we also specifyseverityinsideannotation, it will be annotated
- Inside the
/components/CodeDemo.vue
<script setup>
// ...those imports
const content = `<Button
label="Button"
severity="____severity____">
/>`;
</script>annotationis the config object that specifies what to be and how to annotated- the object key(s) is used to match for the placeholder enclosed with ____ (4 underscore)
- the object value(s) contains detail of how to annotate
- perhaps to provide further explaination (
description) - or perhaps to let users play around with that field (built-in
typeand additionalknob) - you can also specify predefined values to be selected (
options) ...and more!
- perhaps to provide further explaination (
- In the following example, we define an
annotationthat- match for
____severity____insidecontent - replace it with annotation popup that let users play around with the available options
"primary", "secondary", "success" ...etc
- match for
/components/CodeDemo.vue
<script setup>
// ...those imports and content
const annotation = {
severity: {
type: 'string',
knob: 'select',
options: [
'primary',
'secondary',
'success',
'info',
'warning',
'help',
'danger',
],
value: 'secondary',
},
};
</script>- Add interactivity by declaring the ref and the event handler
/components/CodeDemo.vue
<script setup>
// ...those imports, content and annotation
import { ref } from 'vue';
const severity = ref('secondary');
const onValueChange = ({ detail }) => {
const { valueObj } = detail;
severity.value = valueObj.severity;
};
</script>- Pass
languageFn,theme,contentandannotationto<AnnotativeCode />
Combine the use of @valuechange event listener to add interactivity,
you can capture the changes and reflect it to the button you demo.
Here, we demo a <Button /> from primevue/button.
/components/CodeDemo.vue
<script setup>
import Button from 'primevue/button';
// ...all of the above
</script>
<template>
<div>
<Button label="Button" :severity="severity" />
<hr />
<annotative-code
:content="content"
:annotation="annotation"
:languageFn="languageFn"
:themeCss="themeCss"
@valuechange="onValueChange"
/>
</div>
</template>will become...