Cannot read properties of null (reading 'offset') on custom handler getSelection()
I was trying to implement a custom handler for image loading, which gave me the following error:
quill.js?v=ad3ea11b:12611 Uncaught TypeError: Cannot read properties of null (reading 'offset')
at quill.js?v=ad3ea11b:12611:26
at Array.map (
I downgraded to 1.3.7 from 2.0.2 (also tested 2.0.1 and 2.0.0 with no success) and the same code now works flawlessly.
The Handler:
function imageHandler () { if (quill.value) { quill.value.focus() const range = quill.value.getSelection() // this line throws error const value = prompt('please copy paste the image url here.') if (value) { quill.value.insertEmbed(range.index, 'image', value, Quill.sources.USER) } } }
all source code of my component:
``
<h2 class="title">Crear Publicación</h2>
<div class="form-wrapper">
<!-- Título -->
<div>
<label for="titulo">Título</label>
<input type="text" id="titulo" name="titulo"
class="mt-2 block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-lg"
placeholder="Ingrese el título">
</div>
<!-- URL de imagen banner -->
<div>
<label for="banner-url" class="block text-lg font-medium text-gray-700">URL Imagen Banner</label>
<input type="url" id="banner-url" name="banner-url"
class="mt-2 block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-lg"
placeholder="Ingrese la URL de la imagen">
</div>
<!-- Bajada -->
<div>
<label for="bajada" class="block text-lg font-medium text-gray-700">Bajada</label>
<textarea id="bajada" name="bajada" rows="4"
class="mt-2 block w-full px-4 py-3 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-lg"
placeholder="Ingrese la bajada"></textarea>
</div>
<!-- Contenido enriquecido -->
<div>
<label for="contenido" class="block text-lg font-medium text-gray-700">Contenido Enriquecido</label>
<Editor v-model="richContent" class="custom-editor" :modules="quillModules" @load="onLoadEditor">
</Editor>
</div>
<!-- Botón de enviar -->
<div class="flex justify-end">
<button type="submit"
class="inline-flex items-center px-6 py-3 border border-transparent text-lg font-medium rounded-lg shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">Publicar</button>
</div>
</div>
``
I ran into a similar issue using vue with refs, also described here: https://github.com/slab/quill/issues/4293
Changing to a plain variable resolved things for me.
// ❌ doesn't work with Quill2 for me
const quillRef = ref<Quill | null>(null)
onMounted(() => {
quillRef.value = new Quill(...)
});
// ✅ works with quill2
let quill: Quill | null = null
onMounted(() => {
quill = new Quill(...)
})
thanks @jxjj , that worked
thanks! that's good!
thanks!! it works
thanks!! and there is another way
// ✅
const quillRef = ref<Quill | null>(null)
onMounted(() => {
quillRef.value = markRaw(new Quill(...))
});
https://vuejs.org/api/reactivity-advanced.html#markraw
The error occurs because of a conflict between Vue 3's reactivity system (Proxy-based) and Quill's internal DOM/selection management.
When using ref, each .value access triggers Vue's reactivity system, which can cause async updates and interfere with Quill's synchronous operations:
// Problem:
const quill = ref(null) // Proxy intercepts -> async -> timing issues
// Solution:
let quill = null // Direct reference, no reactivity wrapper
use shallowRef. 😄
May be due to Vue's reactivity causing unexpected errors inside the Quill instance.
So, If you want use reactivity system while avoiding this error, don't make the Quill instance itself reactive. Such as shallowRef.
I ran into a similar issue using vue with refs, also described here: #4293
Changing to a plain variable resolved things for me.
// ❌ doesn't work with Quill2 for me const quillRef = ref<Quill | null>(null)
onMounted(() => { quillRef.value = new Quill(...) }); // ✅ works with quill2 let quill: Quill | null = null
onMounted(() => { quill = new Quill(...) })
thanks, it works well.