Browse Source

refactor(nc-gui): update useCellUrlConfig composable

pull/3626/head
braks 2 years ago
parent
commit
38a9cb6c44
  1. 38
      packages/nc-gui/components/cell/Url.vue
  2. 2
      packages/nc-gui/components/shared-view/Grid.vue
  3. 2
      packages/nc-gui/components/smartsheet/Grid.vue
  4. 13
      packages/nc-gui/components/virtual-cell/Lookup.vue
  5. 48
      packages/nc-gui/composables/useCellUrlConfig.ts
  6. 3
      packages/nc-gui/pages/[projectType]/view/[viewId].vue

38
packages/nc-gui/components/cell/Url.vue

@ -1,22 +1,35 @@
<script setup lang="ts"> <script setup lang="ts">
import type { VNodeRef } from '@vue/runtime-core' import type { VNodeRef } from '@vue/runtime-core'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import { useI18n } from 'vue-i18n' import {
import { CellUrlDisableOverlayInj, ColumnInj, EditModeInj, computed, inject, isValidURL, ref } from '#imports' CellUrlDisableOverlayInj,
import MiCircleWarning from '~icons/mi/circle-warning' ColumnInj,
EditModeInj,
computed,
inject,
isValidURL,
ref,
useCellUrlConfig,
useI18n,
watch,
} from '#imports'
const { modelValue: value } = defineProps<Props>()
const emit = defineEmits(['update:modelValue'])
const { t } = useI18n()
interface Props { interface Props {
modelValue?: string | null modelValue?: string | null
} }
const { modelValue: value } = defineProps<Props>()
const emit = defineEmits(['update:modelValue'])
const { t } = useI18n()
const column = inject(ColumnInj)! const column = inject(ColumnInj)!
const editEnabled = inject(EditModeInj)! const editEnabled = inject(EditModeInj)!
const disableOverlay = inject(CellUrlDisableOverlayInj) const disableOverlay = inject(CellUrlDisableOverlayInj)
// Used in the logic of when to display error since we are not storing the url if its not valid // Used in the logic of when to display error since we are not storing the url if its not valid
const localState = ref(value) const localState = ref(value)
@ -40,7 +53,8 @@ const url = computed(() => {
return `https://${value}` return `https://${value}`
}) })
const urlOptions = useCellUrlConfig(url)
const { cellUrlOptions } = useCellUrlConfig(url)
const focus: VNodeRef = (el) => (el as HTMLInputElement)?.focus() const focus: VNodeRef = (el) => (el as HTMLInputElement)?.focus()
@ -62,20 +76,20 @@ watch(
<input v-if="editEnabled" :ref="focus" v-model="vModel" class="outline-none text-sm w-full" @blur="editEnabled = false" /> <input v-if="editEnabled" :ref="focus" v-model="vModel" class="outline-none text-sm w-full" @blur="editEnabled = false" />
<nuxt-link <nuxt-link
v-else-if="isValid && !urlOptions?.overlay" v-else-if="isValid && !cellUrlOptions?.overlay"
class="z-3 text-sm underline hover:opacity-75" class="z-3 text-sm underline hover:opacity-75"
:to="url" :to="url"
:target="urlOptions?.behavior === 'replace' ? undefined : '_blank'" :target="cellUrlOptions?.behavior === 'replace' ? undefined : '_blank'"
> >
{{ value }} {{ value }}
</nuxt-link> </nuxt-link>
<nuxt-link <nuxt-link
v-else-if="isValid && !disableOverlay && urlOptions?.overlay" v-else-if="isValid && !disableOverlay && cellUrlOptions?.overlay"
class="z-3 w-full h-full text-center !no-underline hover:opacity-75" class="z-3 w-full h-full text-center !no-underline hover:opacity-75"
:to="url" :to="url"
:target="urlOptions?.behavior === 'replace' ? undefined : '_blank'" :target="cellUrlOptions?.behavior === 'replace' ? undefined : '_blank'"
> >
{{ urlOptions.overlay }} {{ cellUrlOptions.overlay }}
</nuxt-link> </nuxt-link>
<span v-else class="w-9/10 overflow-ellipsis overflow-hidden">{{ value }}</span> <span v-else class="w-9/10 overflow-ellipsis overflow-hidden">{{ value }}</span>

2
packages/nc-gui/components/shared-view/Grid.vue

@ -2,7 +2,6 @@
import type { Ref } from 'vue' import type { Ref } from 'vue'
import type { TableType } from 'nocodb-sdk' import type { TableType } from 'nocodb-sdk'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import { ActiveViewInj, FieldsInj, IsPublicInj, MetaInj, ReadonlyInj, ReloadViewDataHookInj } from '~/context' import { ActiveViewInj, FieldsInj, IsPublicInj, MetaInj, ReadonlyInj, ReloadViewDataHookInj } from '~/context'
const { sharedView, meta, sorts, nestedFilters } = useSharedView() const { sharedView, meta, sorts, nestedFilters } = useSharedView()
@ -10,6 +9,7 @@ const { signedIn } = useGlobal()
const { loadProject } = useProject(meta?.value.project_id) const { loadProject } = useProject(meta?.value.project_id)
const reloadEventHook = createEventHook<void>() const reloadEventHook = createEventHook<void>()
provide(ReloadViewDataHookInj, reloadEventHook) provide(ReloadViewDataHookInj, reloadEventHook)
provide(ReadonlyInj, true) provide(ReadonlyInj, true)
provide(MetaInj, meta) provide(MetaInj, meta)

2
packages/nc-gui/components/smartsheet/Grid.vue

@ -2,7 +2,6 @@
import type { ColumnType } from 'nocodb-sdk' import type { ColumnType } from 'nocodb-sdk'
import { UITypes, isVirtualCol } from 'nocodb-sdk' import { UITypes, isVirtualCol } from 'nocodb-sdk'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import { useI18n } from 'vue-i18n'
import { import {
ActiveViewInj, ActiveViewInj,
CellUrlDisableOverlayInj, CellUrlDisableOverlayInj,
@ -27,6 +26,7 @@ import {
ref, ref,
useEventListener, useEventListener,
useGridViewColumnWidth, useGridViewColumnWidth,
useI18n,
useSmartsheetStoreOrThrow, useSmartsheetStoreOrThrow,
useUIPermission, useUIPermission,
useViewData, useViewData,

13
packages/nc-gui/components/virtual-cell/Lookup.vue

@ -2,7 +2,18 @@
import type { ColumnType, LinkToAnotherRecordType, LookupType } from 'nocodb-sdk' import type { ColumnType, LinkToAnotherRecordType, LookupType } from 'nocodb-sdk'
import { RelationTypes, UITypes, isVirtualCol } from 'nocodb-sdk' import { RelationTypes, UITypes, isVirtualCol } from 'nocodb-sdk'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import { CellUrlDisableOverlayInj, CellValueInj, ColumnInj, MetaInj, ReadonlyInj, computed, inject, provide, useColumn, useMetas } from '#imports' import {
CellUrlDisableOverlayInj,
CellValueInj,
ColumnInj,
MetaInj,
ReadonlyInj,
computed,
inject,
provide,
useColumn,
useMetas,
} from '#imports'
const { metas, getMeta } = useMetas() const { metas, getMeta } = useMetas()

48
packages/nc-gui/composables/useCellUrlConfig.ts

@ -1,48 +1,48 @@
import type { ComputedRef } from 'vue' import type { MaybeRef } from '@vueuse/core'
import { useRoute } from '#imports' import { computed, ref, unref, useInjectionState, useRoute } from '#imports'
export interface CellUrlOptions { export interface CellUrlOptions {
behavior?: string behavior?: string
overlay?: string overlay?: string
} }
const parseUrlRules = (serialized: string | undefined): Array<[RegExp, CellUrlOptions]> | undefined => { type ParsedRules = [RegExp, CellUrlOptions]
const parseUrlRules = (serialized?: string): ParsedRules[] | undefined => {
if (!serialized) return undefined if (!serialized) return undefined
try { try {
const rules: Array<[RegExp, {}]> = Object.entries(JSON.parse(serialized)).map(([key, value]) => [ return Object.entries(JSON.parse(serialized)).map(([key, value]) => [new RegExp(key), value] as ParsedRules)
new RegExp(key),
value as {},
])
return rules
} catch (err) { } catch (err) {
console.error(err) console.error(err)
return undefined return undefined
} }
} }
const [useProvideCellUrlConfig, useCellUrlGeneralConfig] = useInjectionState(() => { export function useCellUrlConfig(url?: MaybeRef<string>) {
const route = useRoute() const route = useRoute()
return { const config = $computed(() => ({
behavior: route.query.url_behavior as string | undefined, behavior: route.query.url_behavior as string | undefined,
overlay: route.query.url_overlay as string | undefined, overlay: route.query.url_overlay as string | undefined,
rules: parseUrlRules(route.query.url_rules as string), rules: parseUrlRules(route.query.url_rules as string),
} }))
}, 'cell-url-config')
const options = computed(() => {
export { useProvideCellUrlConfig } const options = { behavior: config.behavior, overlay: config.overlay }
export function useCellUrlConfig(url: ComputedRef<string>) { if (config.rules && (!config.behavior || !config.overlay)) {
const config = useCellUrlGeneralConfig() for (const [regex, value] of config.rules) {
if (!config) return undefined if (unref(url)?.match(regex)) return Object.assign(options, value)
return computed(() => {
const { behavior, overlay, rules } = config
const options = { behavior, overlay }
if (rules && (!behavior || !overlay)) {
for (const [regex, value] of rules) {
if (url.value.match(regex)) return Object.assign(options, value)
} }
} }
return options return options
}) })
return {
cellUrlConfig: config,
cellUrlOptions: options,
}
} }

3
packages/nc-gui/pages/[projectType]/view/[viewId].vue

@ -8,7 +8,6 @@ import {
extractSdkResponseErrorMsg, extractSdkResponseErrorMsg,
provide, provide,
ref, ref,
useProvideCellUrlConfig,
useRoute, useRoute,
useSharedView, useSharedView,
} from '#imports' } from '#imports'
@ -25,8 +24,6 @@ const reloadEventHook = createEventHook<void>()
provide(ReloadViewDataHookInj, reloadEventHook) provide(ReloadViewDataHookInj, reloadEventHook)
provide(ReadonlyInj, true) provide(ReadonlyInj, true)
useProvideCellUrlConfig()
const { loadSharedView } = useSharedView() const { loadSharedView } = useSharedView()
const showPassword = ref(false) const showPassword = ref(false)

Loading…
Cancel
Save