My issue is that I didn't get how to implement Vue.set and array.splice on my code to avoid Javascript caveats.
Here is what I'm trying to do:
I highlight a text with my mouse or trackpad and on mouseup the highlighted text is stored on an array of objects. Each object contains the selected text.
I wish to loop on that array to be able to display all selection one by one as long as I'm selecting different text.
Basically, I'm storing each selected text to selectionArray. Each selectedText is a string inside an object. So SelectionArray becomes an array of objects like this On the first selection:
[
{selectedText: '...string...'}
]
On the second selection the array is updated:
[
{selectedText: '...string...'},
{selectedText: '...another string...'}
]
And so on... At the end, I loop with v-on on items array which is equal to selectionArray with:
this.items = selectionArray
For the moment I'm almost there but Vue can't detect addition/deletion of array items and object properties. Caveats section of the Vue guide explain it, but I didn't get how to do it as I'm pretty new with Vue js. Any help please?
Here is the code:
<template>
<main class='wrapper'>
<section class='wrapper-copy'>
<div class='copy'>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis sequi dolorum soluta pariatur asperiores. Recusandae atque nesciunt ipsa velit impedit fugit enim, quia explicabo adipisci sunt earum laudantium illo. Tenetur.
Animi magnam corrupti atque mollitia eaque enim, voluptatum magni laboriosam vel possimus reprehenderit aut doloribus inventore repellat aliquam voluptatem esse ut saepe at iusto qui quibusdam doloremque exercitationem ipsam. Dicta.
In animi nobis accusamus nemo repellat dicta a repellendus provident accusantium fugit voluptas minus laudantium reiciendis cumque, amet porro maiores quisquam? Ullam aut voluptatem delectus cum rerum perferendis vero laudantium!
</div>
</section>
<article class="wrapper-select">
<div class="select">
<div id='input'
class='selected-copy'
v-for='(item, index) in items'
:key='item.index'>
<div class='index'>{{ index }} </div>
<p class='selection'> {{ item.selectedText }} </p>
</div>
</div>
</article>
</main>
</template>
<script>
export default {
name: 'app',
data () {
return {
items: []
}
},
created () {
var selectionArray = []
function storeSelection () {
var selectedText = window.getSelection().toString()
if (selectedText.length && selectionArray.indexOf(selectedText) === -1) {
selectionArray[selectionArray.length] = {selectedText}
}
console.log(selectionArray)
}
document.addEventListener('mouseup', storeSelection)
this.items = selectionArray
console.log(this.items)
}
}
</script>