Git Product home page Git Product logo

Comments (2)

michaelpumo avatar michaelpumo commented on June 2, 2024

I have a solution but it's a bit of a hack, it would be great to have an official way of doing this:

JavaScript

function isStoryblokEditor() {
  const urlParams = new URLSearchParams(window.location.search);
  return urlParams.has('_storyblok');
}

if (isStoryblokEditor()) {
  document.body.classList.add('is-storyblok-editor')
}

Use this to add a class to the body and then have CSS to disable all links.

CSS

body.is-storyblok-editor a {
  pointer-events: none !important;
}

from storyblok-nuxt.

codeflorist avatar codeflorist commented on June 2, 2024

using a seperate component like this instead of NuxtLink throughout your app is also a solution:

// e.g. /components/global/AppLink.vue
<script setup lang="ts">
const route = useRoute()

const isClickForbidden = ref(false)

onMounted(() => {
	const isStoryblokEditor = !!route.query?._storyblok
	if (isStoryblokEditor) {
		isClickForbidden.value = true
	}
})

const showClickForbiddenAlert = () => {
	// eslint-disable-next-line no-alert
	alert('Links are not clickable in the Storyblok editor. Please use the Storyblok editor navigation.')
}
</script>

<template>
	<NuxtLink :class="{ relative: isClickForbidden }">
		<div v-if="isClickForbidden" class="absolute inset-0 z-50 size-full cursor-not-allowed" @click.stop.prevent="showClickForbiddenAlert" />
		<slot />
	</NuxtLink>
</template>

classes are tailwind classes, but should be self-explainable.


here is a composable for doing the same within a renderRichText functionality:

// e.g. /utils/renderStoryblokRichText.ts
import cloneDeep from 'clone-deep'
import { type ISbRichtext, RichTextSchema } from '@storyblok/js'

export const renderStoryblokRichText = () => {
	const { localePathByUuid } = useStoryRoutes()

	const route = useRoute()
	const isStoryblokEditor = !!route.query?._storyblok

	const mySchema = cloneDeep(RichTextSchema)

	mySchema.marks.link = (node) => {
		const attrs = { ...node.attrs }
		const { linktype = 'url' } = node.attrs

		if (linktype === 'email') {
			attrs.href = `mailto:${attrs.href}`
		}

		if (linktype === 'story') {
			attrs.href = attrs.href
		}

		if (attrs.anchor) {
			attrs.href = `${attrs.href}#${attrs.anchor}`
			delete attrs.anchor
		}

		if (isStoryblokEditor) {
			attrs.onclick = "event.preventDefault(); alert('Links are not clickable in the Storyblok editor. Please use the Storyblok editor navigation.');"
		}

		return {
			tag: [
				{
					tag: 'a',
					attrs,
				},
			],
		}
	}

	const render = (richText: ISbRichtext) => {
		return renderRichText(richText, {
			schema: mySchema,
		})
	}

	return {
		render,
	}
}

from storyblok-nuxt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.