diff --git a/devui/anchor/index.ts b/devui/anchor/index.ts index 3df6a901d6e4e5fff5864f8a5273893ebdcc231c..99bd1f054549fab3b4cc4c834a1fb589087de8ac 100644 --- a/devui/anchor/index.ts +++ b/devui/anchor/index.ts @@ -5,20 +5,10 @@ import dAnchorLink from './src/d-anchor-link' import dAnchor from './src/d-anchor' import './src/anchor.scss'; -const directives = { - 'd-anchor': dAnchor, - 'd-anchor-link': dAnchorLink, - 'd-anchor-box': dAnchorBox, - -}; - Anchor.install = function(Vue: App) { - for (const key in directives) { - if (directives.hasOwnProperty(key)) { - - Vue.directive(key, directives[key]); - } - } + Vue.directive(dAnchor.name, dAnchor); + Vue.directive(dAnchorLink.name, dAnchorLink); + Vue.directive(dAnchorBox.name, dAnchorBox); Vue.component(Anchor.name, Anchor) }; diff --git a/devui/anchor/src/d-anchor-box.ts b/devui/anchor/src/d-anchor-box.ts index a9396cd29ea679e467e9d868d77f3ad8344950ae..0ad8a390bc72f553fdc35577e14d0ebd1966eccf 100644 --- a/devui/anchor/src/d-anchor-box.ts +++ b/devui/anchor/src/d-anchor-box.ts @@ -1,5 +1,7 @@ import { setActiveLink, onScroll, randomId } from './util'; +import { inBrowser } from '../../shared/util/common-var'; export default { + name: 'd-anchor-box', // 滚动区域 // 1.监听window滚动或滚动容器滚动,切换link+active,改变# mounted(el: HTMLElement): void { @@ -84,7 +86,7 @@ const cssChange = ( mysidebar.style.left = left + 'px'; }; const addEvent = (function () { - if (window.addEventListener) { + if (inBrowser && window.addEventListener) { return function (elm, type, handle) { elm.addEventListener(type, handle, false); }; diff --git a/devui/anchor/src/d-anchor-link.ts b/devui/anchor/src/d-anchor-link.ts index b4ad15c477885a211ce6f2663ce37e47dd0c6bfb..743106942d1fc6829b6b9dca1cdf1750ac71d493 100644 --- a/devui/anchor/src/d-anchor-link.ts +++ b/devui/anchor/src/d-anchor-link.ts @@ -4,27 +4,28 @@ interface Bind { } export default { - // 当被绑定的元素挂载到 DOM 中时…… - // 1.点击滚动到对应位置,并且高亮 - // 2.到对应位置后,改变url后hash + name: 'd-anchor-link', + // 当被绑定的元素挂载到 DOM 中时…… + // 1.点击滚动到对应位置,并且高亮 + // 2.到对应位置后,改变url后hash - mounted(el: HTMLElement,binding: Bind):void { - const parent: Element = el.parentNode as Element; - if (!parent.className) { - parent.className = 'mysidebar step-nav'; - } - el.className = 'bar-link-item'; - el.innerHTML += '
'; - el.setAttribute('id', binding.value); - - el.onclick = () => { - let scrollContainer: any; - const scollToDomY = document.getElementsByName(binding.value)[0]; - document.getElementsByClassName('scrollTarget').length - ? scrollContainer = document.getElementsByClassName('scrollTarget')[0] - : scrollContainer = window - scrollToControl(scollToDomY, scrollContainer); - - } + mounted(el: HTMLElement,binding: Bind):void { + const parent: Element = el.parentNode as Element; + if (!parent.className) { + parent.className = 'mysidebar step-nav'; } - }; + el.className = 'bar-link-item'; + el.innerHTML += ' '; + el.setAttribute('id', binding.value); + + el.onclick = () => { + let scrollContainer: any; + const scollToDomY = document.getElementsByName(binding.value)[0]; + document.getElementsByClassName('scrollTarget').length + ? scrollContainer = document.getElementsByClassName('scrollTarget')[0] + : scrollContainer = window + scrollToControl(scollToDomY, scrollContainer); + + } + } +}; diff --git a/devui/anchor/src/d-anchor.ts b/devui/anchor/src/d-anchor.ts index 8f6d01c3aafb32103e393062686cf10a13fd1ca4..5f76e8ced1012c4f3f02096e5be54e79eb27b6de 100644 --- a/devui/anchor/src/d-anchor.ts +++ b/devui/anchor/src/d-anchor.ts @@ -4,27 +4,28 @@ interface Bind { } export default { - // 挂载事件到dom - // 1.点击对应link高亮 - // 2.href+#+bing.value + name: 'd-anchor', + // 挂载事件到dom + // 1.点击对应link高亮 + // 2.href+#+bing.value - mounted(el: HTMLElement, binding: Bind):void { - const parent: Element = el.parentNode as Element; - if (!parent.className) { - parent.className = 'mycontent' - } - el.innerHTML = ' ' + el.innerHTML - el.className = 'section-block'; - // anchor-active-by-scroll - el.setAttribute('name',binding.value); - el.onclick = e => { - hightLightFn(binding.value); - - const classList = document.getElementById((e.target as HTMLElement).getAttribute('name')).classList; - console.log(classList) - - } + mounted(el: HTMLElement, binding: Bind):void { + const parent: Element = el.parentNode as Element; + if (!parent.className) { + parent.className = 'mycontent' } - }; + el.innerHTML = ' ' + el.innerHTML + el.className = 'section-block'; + // anchor-active-by-scroll + el.setAttribute('name',binding.value); + el.onclick = e => { + hightLightFn(binding.value); + + const classList = document.getElementById((e.target as HTMLElement).getAttribute('name')).classList; + console.log(classList) + + } + } +}; \ No newline at end of file diff --git a/devui/anchor/src/util.ts b/devui/anchor/src/util.ts index ec4c8ee73c04d55d374e22540dad67d8663045f9..868446b36c41b81e945e1166745a1ac8579876e9 100644 --- a/devui/anchor/src/util.ts +++ b/devui/anchor/src/util.ts @@ -4,7 +4,7 @@ const timeoutIntervalSpeed = 10; let hashName:string; // 滚动是由于点击产生 let scollFlag = false; -function elementPosition(obj: HTMLElement ) { +function elementPosition(obj: HTMLElement) { let curleft = 0, curtop = 0; curleft = obj.offsetLeft; curtop = obj.offsetTop; @@ -12,46 +12,44 @@ function elementPosition(obj: HTMLElement ) { } export function scrollToControl(elem: HTMLElement, container: HTMLElement):void { - hashName = elem.getAttribute('name'); - scollFlag = true; - const tops = container.scrollTop>=0 ? container.scrollTop : -(document.getElementsByClassName('mycontainer')[0] as HTMLElement).offsetTop; - let scrollPos: number = elementPosition(elem).y - tops ; - - scrollPos = scrollPos - document.documentElement.scrollTop; - const remainder: number = scrollPos % timeoutIntervalSpeed; - const repeatTimes = Math.abs((scrollPos - remainder) / timeoutIntervalSpeed); - if (scrollPos < 0 && container || elem.getBoundingClientRect().top < container.offsetTop) { - window.scrollBy(0, elem.getBoundingClientRect().top-container.offsetTop-16) - } - // 多个计时器达到平滑滚动效果 - scrollSmoothly(scrollPos, repeatTimes, container) + hashName = elem.getAttribute('name'); + scollFlag = true; + const tops = container.scrollTop>=0 ? container.scrollTop : -(document.getElementsByClassName('mycontainer')[0] as HTMLElement).offsetTop; + let scrollPos: number = elementPosition(elem).y - tops ; + + scrollPos = scrollPos - document.documentElement.scrollTop; + const remainder: number = scrollPos % timeoutIntervalSpeed; + const repeatTimes = Math.abs((scrollPos - remainder) / timeoutIntervalSpeed); + if (scrollPos < 0 && container || elem.getBoundingClientRect().top < container.offsetTop) { + window.scrollBy(0, elem.getBoundingClientRect().top-container.offsetTop-16) + } + // 多个计时器达到平滑滚动效果 + scrollSmoothly(scrollPos, repeatTimes, container) } function scrollSmoothly(scrollPos: number, repeatTimes: number, container: HTMLElement):void { - - if (repeatCount <= repeatTimes) { - scrollPos > 0 - ? container.scrollBy(0, timeoutIntervalSpeed) - : container.scrollBy(0, -timeoutIntervalSpeed) - } - else { - repeatCount = 0; - clearTimeout(cTimeout); - history.replaceState(null, null, document.location.pathname + '#' + hashName); - - hightLightFn(hashName) - setTimeout(() => { - scollFlag = false; - }, 310) - return ; - - } - repeatCount++; - cTimeout = setTimeout(() => { - scrollSmoothly(scrollPos, repeatTimes, container) - }, 10) + if (repeatCount <= repeatTimes) { + scrollPos > 0 + ? container.scrollBy(0, timeoutIntervalSpeed) + : container.scrollBy(0, -timeoutIntervalSpeed) + } + else { + repeatCount = 0; + clearTimeout(cTimeout); + history.replaceState(null, null, document.location.pathname + '#' + hashName); + hightLightFn(hashName) + setTimeout(() => { + scollFlag = false; + }, 310) + return ; + + } + repeatCount++; + cTimeout = setTimeout(() => { + scrollSmoothly(scrollPos, repeatTimes, container) + }, 10) } // 高亮切换 diff --git a/devui/modal/index.ts b/devui/modal/index.ts index f324c5d93ca4aa857ce61de342fb8505b3597315..4c28caa116072f2125a034f9682f9f39d9976e4e 100644 --- a/devui/modal/index.ts +++ b/devui/modal/index.ts @@ -2,6 +2,7 @@ import type { App } from 'vue' import Modal from './src/modal' import { ModalService } from './src/services/modal-service' import { DialogService } from './src/services/dialog-service' +import { inBrowser } from '../shared/util/common-var' Modal.install = function(app: App): void { app.component(Modal.name, Modal) @@ -16,6 +17,10 @@ export default { install(app: App): void { app.use(Modal as any) + if (!inBrowser) { + return; + } + let anchorsContainer = document.getElementById('d-modal-anchors-container'); if (!anchorsContainer) { anchorsContainer = document.createElement('div'); diff --git a/devui/overlay/index.ts b/devui/overlay/index.ts index e5a97c08ab92f1925f228ee0fd33079862f07f5e..978cc5e3b26eb4a8475958f2b9e920aefb14f289 100644 --- a/devui/overlay/index.ts +++ b/devui/overlay/index.ts @@ -1,6 +1,7 @@ import type { App } from 'vue' import {FixedOverlay} from './src/fixed-overlay'; import {FlexibleOverlay } from './src/flexible-overlay'; +import {inBrowser} from '../shared/util/common-var'; FlexibleOverlay.install = function(app: App) { app.component(FlexibleOverlay.name, FlexibleOverlay); @@ -18,7 +19,8 @@ export default { install(app: App): void { app.use(FixedOverlay as any); app.use(FlexibleOverlay as any); - if (!document.getElementById('d-overlay-anchor')) { + + if (inBrowser && !document.getElementById('d-overlay-anchor')) { const overlayAnchor = document.createElement('div'); overlayAnchor.setAttribute('id', 'd-overlay-anchor'); overlayAnchor.style.position = 'fixed'; diff --git a/devui/overlay/src/utils.ts b/devui/overlay/src/utils.ts index 7e8c34d7d9f22a6a9b801945935c8c2ace1677ca..262253e8f273a532e2f8ce789eb87d3f22a9e70a 100644 --- a/devui/overlay/src/utils.ts +++ b/devui/overlay/src/utils.ts @@ -1,4 +1,4 @@ -import { onUnmounted, watch, computed, ComputedRef } from 'vue'; +import { onUnmounted, watch, computed, ComputedRef, onMounted } from 'vue'; import { OverlayProps } from './overlay-types'; interface CommonInfo { @@ -27,28 +27,28 @@ export function useOverlayLogic(props: OverlayProps): CommonInfo { const handleOverlayBubbleCancel = (event: Event) => (event.cancelBubble = true); - - const body = document.body; - const originOverflow = body.style.overflow; - const originPosition = body.style.position; - watch([() => props.visible, () => props.backgroundBlock], ([visible, backgroundBlock]) => { - if (backgroundBlock) { - const top = body.getBoundingClientRect().y; - if (visible) { - body.style.overflowY = 'scroll'; - body.style.position = visible ? 'fixed' : ''; - body.style.top = `${top}px`; - } else { - body.style.overflowY = originOverflow; - body.style.position = originPosition; - body.style.top = ''; - window.scrollTo(0, -top); + onMounted(() => { + const body = document.body; + const originOverflow = body.style.overflow; + const originPosition = body.style.position; + watch([() => props.visible, () => props.backgroundBlock], ([visible, backgroundBlock]) => { + if (backgroundBlock) { + const top = body.getBoundingClientRect().y; + if (visible) { + body.style.overflowY = 'scroll'; + body.style.position = visible ? 'fixed' : ''; + body.style.top = `${top}px`; + } else { + body.style.overflowY = originOverflow; + body.style.position = originPosition; + body.style.top = ''; + window.scrollTo(0, -top); + } } - } - }); - - onUnmounted(() => { - body.style.overflow = originOverflow; + }); + onUnmounted(() => { + document.body.style.overflow = originOverflow; + }); }); return { diff --git a/devui/shared/devui-directive/clickoutside.ts b/devui/shared/devui-directive/clickoutside.ts index b6b6ad81d252cbe8b0361a4ff2525d6f0b757801..18be40209900771d9e34b0db2d397f6654b2a1ca 100644 --- a/devui/shared/devui-directive/clickoutside.ts +++ b/devui/shared/devui-directive/clickoutside.ts @@ -5,6 +5,7 @@ *