Skip to content

Commit 663387f

Browse files
committed
Extract useBBlockNavigation composable; show source bblock for snippet refs
- Add composables/bblock-navigation.js with openBBlock, canOpenBBlock, getBBlockUrl — shared logic previously duplicated across components - Migrate MarkdownText and TransformInfo to use the composable - ExampleViewer: watch currentSnippet.ref, resolve it via findResource, and display the source bblock (identifier + name, linked) when it differs from the current bblock - ExampleViewer: annotate json-path snippets with the JSONPath expression and a link to jsonpath-ng
1 parent 288e78c commit 663387f

4 files changed

Lines changed: 65 additions & 54 deletions

File tree

src/components/MarkdownText.vue

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,20 @@
22
import {md2html} from "@/lib/utils";
33
import bblockService from "@/services/bblock.service";
44
import {reactive} from "vue";
5-
import { useRouter } from 'vue-router'
5+
import { useBBlockNavigation } from "@/composables/bblock-navigation";
66
77
const props = defineProps({
88
baseUrl: String,
99
content: String,
1010
});
1111
12-
const router = useRouter();
12+
const { openBBlock } = useBBlockNavigation();
1313
1414
const errorMessage = reactive({
1515
visible: false,
1616
text: '',
1717
});
1818
19-
const openBBlock = (bblock, newWindow = false) => {
20-
if (bblockService.isShown(bblock)) {
21-
const route = {
22-
name: 'BuildingBlock',
23-
params: {
24-
id: bblock.itemIdentifier,
25-
},
26-
};
27-
if (newWindow) {
28-
window.open(router.resolve(route).href, '_blank')
29-
} else {
30-
router.push(route);
31-
}
32-
} else if (bblock.documentation?.['bblocks-viewer']) {
33-
const url = bblock.documentation['bblocks-viewer'].url;
34-
if (newWindow) {
35-
window.open(url, '_blank');
36-
} else {
37-
window.location.href = url;
38-
}
39-
}
40-
};
41-
4219
const interceptLinks = (e, newWindow = false) => {
4320
let url = null;
4421
if (e.target?.href) {

src/components/bblock/ExampleViewer.vue

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,20 @@
151151
<v-col cols="12" :md="example.snippets?.length ? 6 : 12" v-if="showContentSidebar">
152152
<MarkdownText class="example-content" :content="example.content" :base-url="sourceFilesUrl"></MarkdownText>
153153
<div v-if="currentSnippetRemote">
154-
This snippet was retrieved from <a :href="currentSnippet.ref" target="_blank">{{ currentSnippet.ref }}</a>.
154+
<template v-if="currentSnippet['json-path']">
155+
This snippet was extracted from <a :href="currentSnippet.ref" target="_blank">{{ currentSnippet.ref }}</a>
156+
using the <a href="https://pypi.org/project/jsonpath-ng/" target="_blank">JSONPath</a>
157+
expression <code>{{ currentSnippet['json-path'] }}</code>.
158+
</template>
159+
<template v-else>
160+
This snippet was retrieved from <a :href="currentSnippet.ref" target="_blank">{{ currentSnippet.ref }}</a>.
161+
</template>
162+
<template v-if="refBBlock && refBBlock.itemIdentifier !== bblock.itemIdentifier">
163+
<span v-if="canOpenBBlock(refBBlock)">
164+
(from <a :href="getBBlockUrl(refBBlock)" @click.prevent="openBBlock(refBBlock)"><code>{{ refBBlock.itemIdentifier }}</code> - {{ refBBlock.name }}</a>)
165+
</span>
166+
<span v-else>(from <code>{{ refBBlock.itemIdentifier }}</code> - {{ refBBlock.name }})</span>
167+
</template>
155168
</div>
156169
</v-col>
157170
<v-col cols="12" :md="showContentSidebar ? 6 : 12" v-if="example.snippets?.length">
@@ -242,7 +255,6 @@
242255
</template>
243256
<script setup>
244257
import { computed, reactive, ref, watch } from 'vue';
245-
import { useRouter } from 'vue-router';
246258
import CodeViewer from "@/components/CodeViewer.vue";
247259
import JsonLdIcon from '@/assets/json-ld-data-white.svg';
248260
import MarkdownText from "@/components/MarkdownText.vue";
@@ -251,6 +263,7 @@ import TransformInfo from "@/components/bblock/TransformInfo.vue";
251263
import { geoJsonLanguageIds } from "@/models/mime-types";
252264
import { getTypeColor } from "@/models/transforms";
253265
import { useFetchDocumentByUrl } from "@/composables/bblock";
266+
import { useBBlockNavigation } from "@/composables/bblock-navigation";
254267
import CopyToClipboardButton from "@/components/CopyToClipboardButton.vue";
255268
import bblockService from "@/services/bblock.service";
256269
@@ -261,13 +274,14 @@ const props = defineProps({
261274
sourceFilesUrl: String,
262275
});
263276
264-
const router = useRouter();
277+
const { openBBlock, canOpenBBlock, getBBlockUrl } = useBBlockNavigation();
265278
266279
const fullscreen = ref(false);
267280
const showTransformDetails = ref(false);
268281
const transformOutputView = ref('code');
269282
const profilesMenuVisible = ref(false);
270283
const profileBBlocks = ref({});
284+
const refBBlock = ref(null);
271285
272286
watch(() => props.language, () => {
273287
showTransformDetails.value = false;
@@ -305,6 +319,13 @@ const currentSnippetRemote = computed(() =>
305319
currentSnippet.value?.ref && /^https?:\/\//.test(currentSnippet.value.ref)
306320
);
307321
322+
watch(currentSnippet, async (snippet) => {
323+
refBBlock.value = null;
324+
if (snippet?.ref) {
325+
refBBlock.value = await bblockService.findResource(snippet.ref) ?? null;
326+
}
327+
}, { immediate: true });
328+
308329
const showContentSidebar = computed(() =>
309330
props.example.content?.trim() || currentSnippetRemote.value
310331
);

src/components/bblock/TransformInfo.vue

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@
139139
</template>
140140
<script setup>
141141
import { ref, watch, onMounted } from 'vue';
142-
import { useRouter } from 'vue-router';
143142
import CodeViewer from "@/components/CodeViewer.vue";
144143
import CopyToClipboardButton from "@/components/CopyToClipboardButton.vue";
145144
import MarkdownText from "@/components/MarkdownText.vue";
146145
import bblockService from "@/services/bblock.service";
147146
import { getTypeColor, getCodeLanguage } from "@/models/transforms";
147+
import { useBBlockNavigation } from "@/composables/bblock-navigation";
148148
149149
const props = defineProps({
150150
transform: { type: Object, required: true },
@@ -153,7 +153,7 @@ const props = defineProps({
153153
sourceFilesUrl: { type: String, default: null },
154154
});
155155
156-
const router = useRouter();
156+
const { openBBlock, canOpenBBlock, getBBlockUrl } = useBBlockNavigation();
157157
const toArray = v => !v ? [] : (Array.isArray(v) ? v : [v]);
158158
159159
const pluginByType = ref({});
@@ -180,30 +180,9 @@ watch(() => props.transform?.outputs?.profiles, async (profiles) => {
180180
outputProfileBBlocks.value = result;
181181
}, { immediate: true });
182182
183-
function canOpenOutputProfile(uri) {
184-
const bblock = outputProfileBBlocks.value[uri];
185-
if (!bblock) return false;
186-
return bblockService.isShown(bblock) || !!bblock.documentation?.['bblocks-viewer'];
187-
}
188-
189-
function getOutputProfileUrl(uri) {
190-
const bblock = outputProfileBBlocks.value[uri];
191-
if (!bblock) return undefined;
192-
if (bblockService.isShown(bblock)) {
193-
return router.resolve({ name: 'BuildingBlock', params: { id: bblock.itemIdentifier } }).href;
194-
}
195-
return bblock.documentation?.['bblocks-viewer']?.url;
196-
}
197-
198-
function openOutputProfile(uri) {
199-
const bblock = outputProfileBBlocks.value[uri];
200-
if (!bblock) return;
201-
if (bblockService.isShown(bblock)) {
202-
router.push({ name: 'BuildingBlock', params: { id: bblock.itemIdentifier } });
203-
} else if (bblock.documentation?.['bblocks-viewer']) {
204-
window.open(bblock.documentation['bblocks-viewer'].url);
205-
}
206-
}
183+
function canOpenOutputProfile(uri) { return canOpenBBlock(outputProfileBBlocks.value[uri]); }
184+
function getOutputProfileUrl(uri) { return getBBlockUrl(outputProfileBBlocks.value[uri]); }
185+
function openOutputProfile(uri) { openBBlock(outputProfileBBlocks.value[uri]); }
207186
</script>
208187
<style scoped lang="scss">
209188
.transform-header {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { useRouter } from 'vue-router';
2+
import bblockService from '@/services/bblock.service';
3+
4+
export function useBBlockNavigation() {
5+
const router = useRouter();
6+
7+
function getBBlockUrl(bblock) {
8+
if (bblockService.isShown(bblock)) {
9+
return router.resolve({ name: 'BuildingBlock', params: { id: bblock.itemIdentifier } }).href;
10+
} else if (bblock.documentation?.['bblocks-viewer']) {
11+
return bblock.documentation['bblocks-viewer'].url;
12+
}
13+
return null;
14+
}
15+
16+
function canOpenBBlock(bblock) {
17+
return bblockService.isShown(bblock) || !!bblock.documentation?.['bblocks-viewer'];
18+
}
19+
20+
function openBBlock(bblock, newWindow = false) {
21+
if (bblockService.isShown(bblock)) {
22+
const route = { name: 'BuildingBlock', params: { id: bblock.itemIdentifier } };
23+
if (newWindow) {
24+
window.open(router.resolve(route).href, '_blank');
25+
} else {
26+
router.push(route);
27+
}
28+
} else if (bblock.documentation?.['bblocks-viewer']) {
29+
window.open(bblock.documentation['bblocks-viewer'].url, '_blank');
30+
}
31+
}
32+
33+
return { getBBlockUrl, canOpenBBlock, openBBlock };
34+
}

0 commit comments

Comments
 (0)