hexo-theme-shokax 0.4.0-alpha.1 → 0.4.0-alpha.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. package/_config.yml +5 -4
  2. package/languages/README.md +22 -0
  3. package/languages/ar.yml +153 -0
  4. package/languages/de.yml +153 -0
  5. package/languages/es.yml +153 -0
  6. package/languages/fr.yml +153 -0
  7. package/languages/hi.yml +153 -0
  8. package/languages/id.yml +153 -0
  9. package/languages/it.yml +153 -0
  10. package/languages/ko.yml +153 -0
  11. package/languages/nl.yml +153 -0
  12. package/languages/pl.yml +153 -0
  13. package/languages/pt.yml +153 -0
  14. package/languages/ru.yml +153 -0
  15. package/languages/tr.yml +153 -0
  16. package/languages/vi.yml +153 -0
  17. package/layout/_mixin/comment.pug +2 -37
  18. package/layout/_partials/footer.pug +1 -1
  19. package/layout/_partials/head/head.pug +7 -3
  20. package/layout/_partials/header.pug +1 -1
  21. package/layout/_partials/layout.pug +1 -10
  22. package/layout/_partials/post/footer.pug +0 -9
  23. package/package.json +8 -2
  24. package/scripts/generaters/archive.js +1 -1
  25. package/scripts/generaters/script.js +63 -39
  26. package/scripts/helpers/asset.js +3 -72
  27. package/scripts/helpers/list_categories.js +0 -4
  28. package/scripts/plugin/lib/injects.js +1 -1
  29. package/source/js/_app/components/comments.ts +60 -0
  30. package/source/js/_app/components/sidebar.ts +7 -5
  31. package/source/js/_app/components/tcomments.ts +41 -0
  32. package/source/js/_app/globals/globalVars.ts +1 -0
  33. package/source/js/_app/globals/handles.ts +6 -5
  34. package/source/js/_app/globals/themeColor.ts +1 -1
  35. package/source/js/_app/globals/thirdparty.ts +1 -1
  36. package/source/js/_app/globals/tools.ts +3 -2
  37. package/source/js/_app/library/anime.ts +6 -5
  38. package/source/js/_app/library/declare.d.ts +25 -9
  39. package/source/js/_app/library/loadFile.ts +3 -1
  40. package/source/js/_app/library/proto.ts +75 -77
  41. package/source/js/_app/library/vue.ts +38 -48
  42. package/source/js/_app/page/fancybox.ts +2 -1
  43. package/source/js/_app/page/post.ts +4 -3
  44. package/source/js/_app/page/search.ts +17 -11
  45. package/source/js/_app/page/tab.ts +3 -2
  46. package/source/js/_app/pjax/domInit.ts +5 -4
  47. package/source/js/_app/pjax/refresh.ts +20 -2
  48. package/source/js/_app/pjax/siteInit.ts +12 -7
  49. package/source/js/_app/player.ts +16 -15
  50. package/scripts/filters/locals.d.ts +0 -1
  51. package/scripts/filters/locals.ts +0 -59
  52. package/scripts/filters/post.d.ts +0 -0
  53. package/scripts/filters/post.ts +0 -6
  54. package/scripts/generaters/archive.d.ts +0 -1
  55. package/scripts/generaters/archive.ts +0 -144
  56. package/scripts/generaters/config.d.ts +0 -1
  57. package/scripts/generaters/config.ts +0 -52
  58. package/scripts/generaters/images.d.ts +0 -1
  59. package/scripts/generaters/images.ts +0 -26
  60. package/scripts/generaters/index.d.ts +0 -1
  61. package/scripts/generaters/index.ts +0 -110
  62. package/scripts/generaters/pages.d.ts +0 -0
  63. package/scripts/generaters/pages.ts +0 -16
  64. package/scripts/generaters/script.d.ts +0 -1
  65. package/scripts/generaters/script.ts +0 -110
  66. package/scripts/helpers/asset.d.ts +0 -1
  67. package/scripts/helpers/asset.ts +0 -158
  68. package/scripts/helpers/engine.d.ts +0 -1
  69. package/scripts/helpers/engine.ts +0 -171
  70. package/scripts/helpers/list_categories.d.ts +0 -1
  71. package/scripts/helpers/list_categories.ts +0 -104
  72. package/scripts/helpers/summary_ai.d.ts +0 -1
  73. package/scripts/helpers/summary_ai.ts +0 -100
  74. package/scripts/helpers/symbols_count_time.d.ts +0 -1
  75. package/scripts/helpers/symbols_count_time.ts +0 -76
  76. package/scripts/plugin/check.d.ts +0 -1
  77. package/scripts/plugin/check.ts +0 -35
  78. package/scripts/plugin/index.d.ts +0 -6
  79. package/scripts/plugin/index.ts +0 -52
  80. package/scripts/plugin/lib/injects-point.d.ts +0 -5
  81. package/scripts/plugin/lib/injects-point.ts +0 -20
  82. package/scripts/plugin/lib/injects.d.ts +0 -2
  83. package/scripts/plugin/lib/injects.ts +0 -101
  84. package/scripts/tags/links.d.ts +0 -1
  85. package/scripts/tags/links.ts +0 -75
  86. package/scripts/tags/media.d.ts +0 -1
  87. package/scripts/tags/media.ts +0 -19
  88. package/source/assets/beian.webp +0 -0
  89. package/source/js/_app/library/libtype.d.ts +0 -4
@@ -7,78 +7,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const package_json_1 = __importDefault(require("../../package.json"));
8
8
  const hexo_util_1 = require("hexo-util");
9
9
  const utils_1 = require("../utils");
10
+ // TODO 弃用函数
10
11
  hexo.extend.helper.register('_new_comments', function (mode) {
11
- const root = this.config.url.replace(/^(https?:\/\/)?[^\/]*/, '');
12
- if (mode === 'twikoo') {
13
- return `<script data-pjax type="module">
14
- let comments = []
15
- twikoo.getRecentComments({
16
- envId: "${hexo.theme.config?.twikoo?.envId}",
17
- pageSize: 10
18
- }).then(function (res) {
19
- res.forEach(function (item) {
20
- let cText = item.commentText
21
- if (item.commentText.length > 50) {
22
- cText = item.commentText.substring(0,50)+'...'
23
- }
24
- const siteLink = item.url + "#" + item.id
25
- comments.push({
26
- href: siteLink,
27
- nick: item.nick,
28
- time: item.relativeTime,
29
- text: cText
30
- })
31
- });
32
- Vue.createApp({
33
- data() {
34
- return {
35
- coms: comments,
36
- root: '${root}'
37
- }
38
- }
39
- }).mount('#new-comment')
40
- }).catch(function (err) {
41
- console.error(err)
42
- })
43
- </script>`;
44
- }
45
- else if (mode === 'waline') {
46
- return `
47
- <script type="module" data-pjax>
48
- let items = []
49
- import { RecentComments } from 'https://npm.webcache.cn/@waline/client@v2/dist/waline.mjs'
50
- RecentComments({
51
- serverURL: '${hexo.theme.config.waline.serverURL.replace(/\/+$/, '')}',
52
- count: 10,
53
- }).then(({ comments }) => {
54
- comments.forEach(function (item) {
55
- let cText = (item.orig.length > 50) ? item.orig.substring(0,50)+'...' : item.orig
56
- item.url = item.url.startsWith('/') ? item.url : '/' + item.url;
57
- const siteLink = item.url + "#" + item.objectId
58
- items.push({
59
- href: siteLink,
60
- nick: item.nick,
61
- time: item.insertedAt.split('T').shift(),
62
- text: cText
63
- })
64
- })
65
- Vue.createApp({
66
- data() {
67
- return {
68
- coms: items,
69
- root: '${root}'
70
- }
71
- }
72
- }).mount('#new-comment')
73
- }).catch(function (err) {
74
- console.error(err)
75
- })
76
- </script>
77
- `;
78
- }
79
- else {
80
- console.log(`${mode} is not supported recent comment`);
81
- }
12
+ return '';
82
13
  });
83
14
  hexo.extend.helper.register('_safedump', (source) => {
84
15
  return JSON.stringify(source);
@@ -126,7 +57,7 @@ hexo.extend.helper.register('_css', function (...urls) {
126
57
  });
127
58
  hexo.extend.helper.register('_js', function (...urls) {
128
59
  const { statics, js } = hexo.theme.config;
129
- return urls.map(url => (0, hexo_util_1.htmlTag)('script', { src: hexo_util_1.url_for.call(this, `${statics}${js}/${url}?v=${package_json_1.default.version}`) }, '')).join('');
60
+ return urls.map(url => (0, hexo_util_1.htmlTag)('script', { src: hexo_util_1.url_for.call(this, `${statics}${js}/${url}?v=${package_json_1.default.version}`), type: 'module', fetchpriority: 'high' }, '')).join('');
130
61
  });
131
62
  hexo.extend.helper.register('vendor_js', function () {
132
63
  const vendors = hexo.theme.config.vendors;
@@ -1,5 +1,3 @@
1
- 'use strict';
2
- /* global hexo */
3
1
  const prepareQuery = (categories, parent) => {
4
2
  const query = {
5
3
  parent: undefined
@@ -13,7 +11,6 @@ const prepareQuery = (categories, parent) => {
13
11
  return categories.find(query).sort('name', 1).filter(cat => cat.length);
14
12
  };
15
13
  hexo.extend.helper.register('_list_categories', function (depth = 0) {
16
- // let hexo = this
17
14
  const categories = this.site.categories;
18
15
  if (!categories || !categories.length)
19
16
  return '';
@@ -48,7 +45,6 @@ hexo.extend.helper.register('_list_categories', function (depth = 0) {
48
45
  return hierarchicalList(0);
49
46
  });
50
47
  hexo.extend.helper.register('_category_prev', function (name) {
51
- // let hexo = this
52
48
  const categories = this.site.categories;
53
49
  if (!categories || !categories.length)
54
50
  return '';
@@ -1,4 +1,4 @@
1
- 'use strict';
1
+ "use strict";
2
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
@@ -0,0 +1,60 @@
1
+ import { CONFIG } from '../globals/globalVars'
2
+ import { init, pageviewCount, RecentComments } from '@waline/client'
3
+
4
+ import { createApp } from 'vue'
5
+
6
+ // await import('@waline/client/style')
7
+ // fixme 处理样式引入问题
8
+
9
+ export const walineComment = function () {
10
+ init({
11
+ el: '#comments',
12
+ serverURL: CONFIG.waline.serverURL,
13
+ lang: CONFIG.waline.lang,
14
+ locale: CONFIG.waline.locale,
15
+ emoji: CONFIG.waline.emoji,
16
+ meta: CONFIG.waline.meta,
17
+ requiredMeta: CONFIG.waline.requiredMeta,
18
+ wordLimit: CONFIG.waline.wordLimit,
19
+ pageSize: CONFIG.waline.pageSize,
20
+ pageview: CONFIG.waline.pageview,
21
+ path: window.location.pathname,
22
+ dark: 'html[data-theme="dark"]'
23
+ })
24
+ }
25
+
26
+ export const walinePageview = function () {
27
+ pageviewCount({
28
+ serverURL: CONFIG.waline.serverURL,
29
+ path: window.location.pathname
30
+ })
31
+ }
32
+
33
+ export const walineRecentComments = async function () {
34
+ const root = shokax_siteURL.replace(/^(https?:\/\/)?[^/]*/, '')
35
+ let items = []
36
+ const { comments } = await RecentComments({
37
+ serverURL: CONFIG.waline.serverURL.replace(/\/+$/, ''),
38
+ count: 10
39
+ })
40
+ comments.forEach(function (item) {
41
+ let cText = (item.orig.length > 50) ? item.orig.substring(0, 50) + '...' : item.orig
42
+ item.url = item.url.startsWith('/') ? item.url : '/' + item.url
43
+ const siteLink = item.url + '#' + item.objectId
44
+ items.push({
45
+ href: siteLink,
46
+ nick: item.nick,
47
+ // @ts-ignore
48
+ time: item.insertedAt.split('T').shift(),
49
+ text: cText
50
+ })
51
+ })
52
+ createApp({
53
+ data () {
54
+ return {
55
+ coms: items,
56
+ root
57
+ }
58
+ }
59
+ }).mount('#new-comment')
60
+ }
@@ -1,10 +1,12 @@
1
1
  /* 边栏分区 */
2
2
 
3
- import { Container, diffY, menuToggle, showContents, sideBar } from '../globals/globalVars'
3
+ import { CONFIG, Container, diffY, menuToggle, showContents, sideBar } from '../globals/globalVars'
4
4
  import { clipBoard } from '../globals/tools'
5
5
  import { pageScroll, transition } from '../library/anime'
6
6
  import { $dom } from '../library/dom'
7
+ import initProto, { child, getHeight, setDisplay } from '../library/proto'
7
8
 
9
+ initProto()
8
10
  export const sideBarToggleHandle = (event:Event, force?:number) => {
9
11
  if (sideBar.hasClass('on')) {
10
12
  sideBar.removeClass('on')
@@ -31,7 +33,7 @@ export const sideBarToggleHandle = (event:Event, force?:number) => {
31
33
  }
32
34
 
33
35
  export const sideBarTab = () => {
34
- const sideBarInner = sideBar.child('.inner')
36
+ const sideBarInner = child(sideBar, '.inner')
35
37
 
36
38
  if (sideBar.child('.tab')) {
37
39
  sideBarInner.removeChild(sideBar.child('.tab'))
@@ -45,13 +47,13 @@ export const sideBarTab = () => {
45
47
 
46
48
  if (element.innerHTML.trim().length < 1) {
47
49
  if (item === 'contents') {
48
- showContents.display('none')
50
+ setDisplay(showContents, 'none')
49
51
  }
50
52
  return
51
53
  }
52
54
 
53
55
  if (item === 'contents') {
54
- showContents.display('')
56
+ setDisplay(showContents, '')
55
57
  }
56
58
 
57
59
  const tab = document.createElement('li')
@@ -212,7 +214,7 @@ export const backToTopHandle = () => {
212
214
  }
213
215
 
214
216
  export const goToBottomHandle = () => {
215
- pageScroll(parseInt(String(Container.changeOrGetHeight())))
217
+ pageScroll(parseInt(String(getHeight(Container))))
216
218
  }
217
219
 
218
220
  export const goToCommentHandle = () => {
@@ -0,0 +1,41 @@
1
+ import twikoo from 'twikoo'
2
+ import { CONFIG } from '../globals/globalVars'
3
+ import { createApp } from 'vue'
4
+
5
+ export const twikooComment = function () {
6
+ twikoo.init({
7
+ envId: CONFIG.twikoo.envId,
8
+ el: '#comments',
9
+ region: CONFIG.twikoo.region
10
+ })
11
+ }
12
+
13
+ export const twikooRecentComments = async function () {
14
+ let comments = []
15
+ const root = shokax_siteURL.replace(/^(https?:\/\/)?[^/]*/, '')
16
+ const res = await twikoo.getRecentComments({
17
+ envId: CONFIG.twikoo.envId,
18
+ pageSize: 10
19
+ })
20
+ res.forEach(function (item) {
21
+ let cText = item.commentText
22
+ if (item.commentText.length > 50) {
23
+ cText = item.commentText.substring(0, 50) + '...'
24
+ }
25
+ const siteLink = item.url + '#' + item.id
26
+ comments.push({
27
+ href: siteLink,
28
+ nick: item.nick,
29
+ time: item.relativeTime,
30
+ text: cText
31
+ })
32
+ })
33
+ createApp({
34
+ data () {
35
+ return {
36
+ coms: comments,
37
+ root
38
+ }
39
+ }
40
+ }).mount('#new-comment')
41
+ }
@@ -2,6 +2,7 @@ import { $dom } from '../library/dom'
2
2
  import Pjax from 'theme-shokax-pjax'
3
3
  import initProto from '../library/proto'
4
4
 
5
+ export const CONFIG = shokax_CONFIG
5
6
  initProto()
6
7
  export const statics = CONFIG.statics.indexOf('//') > 0 ? CONFIG.statics : CONFIG.root
7
8
  export const scrollAction: { x: number, y: number } = { x: 0, y: 0 }
@@ -18,18 +18,19 @@ import {
18
18
  setSiteNavHeight,
19
19
  setHeaderHightInner,
20
20
  setHeaderHight,
21
- setOWinHeight, setOWinWidth, setDiffY, setTitleTime
21
+ setOWinHeight, setOWinWidth, setDiffY, setTitleTime, CONFIG
22
22
  } from './globalVars'
23
23
  import { changeMetaTheme } from './themeColor'
24
24
  import { Loader } from './thirdparty'
25
+ import {getHeight, setWidth} from '../library/proto'
25
26
 
26
27
  export const resizeHandle = () => {
27
28
  // 获取 siteNav 的高度
28
- setSiteNavHeight(siteNav.changeOrGetHeight())
29
+ setSiteNavHeight(getHeight(siteNav))
29
30
  // 获取 siteHeader 的高度
30
- setHeaderHightInner(siteHeader.changeOrGetHeight())
31
+ setHeaderHightInner(getHeight(siteHeader))
31
32
  // 获取 #waves 的高度
32
- setHeaderHight(headerHightInner + $dom('#waves').changeOrGetHeight())
33
+ setHeaderHight(headerHightInner + getHeight($dom('#waves')))
33
34
 
34
35
  // 判断窗口宽度是否改变
35
36
  if (oWinWidth !== window.innerWidth) {
@@ -90,7 +91,7 @@ export const scrollHandle = () => {
90
91
  }
91
92
  // 更新百分比进度条的宽度
92
93
  if ($dom('#sidebar').hasClass('affix') || $dom('#sidebar').hasClass('on')) {
93
- $dom('.percent').changeOrGetWidth(scrollPercent)
94
+ setWidth($dom('.percent'), scrollPercent)
94
95
  }
95
96
  }
96
97
 
@@ -1,6 +1,6 @@
1
1
  import { $storage } from '../library/storage'
2
2
  import { $dom } from '../library/dom'
3
- import { HTML } from './globalVars'
3
+ import { CONFIG, HTML } from './globalVars'
4
4
 
5
5
  /**
6
6
  * 更改日夜模式
@@ -1,6 +1,6 @@
1
1
  // 与第三方js的交互或第三方嵌入js
2
2
 
3
- import { loadCat } from './globalVars'
3
+ import { CONFIG, loadCat } from './globalVars'
4
4
  import { transition } from '../library/anime'
5
5
 
6
6
  // 加载动画
@@ -1,7 +1,8 @@
1
1
  import { pageScroll } from '../library/anime'
2
2
  import { $dom } from '../library/dom'
3
3
  import { $storage } from '../library/storage'
4
- import { BODY, LOCAL_HASH, LOCAL_URL, scrollAction, setLocalHash } from './globalVars'
4
+ import { BODY, CONFIG, LOCAL_HASH, LOCAL_URL, scrollAction, setLocalHash } from './globalVars'
5
+ import { createChild } from '../library/proto'
5
6
 
6
7
  // 显示提示(现阶段用于版权及复制结果提示)
7
8
  export const showtip = (msg: string): void | never => {
@@ -9,7 +10,7 @@ export const showtip = (msg: string): void | never => {
9
10
  return
10
11
  }
11
12
 
12
- const tipbox = BODY.createChild('div', {
13
+ const tipbox = createChild(BODY, 'div', {
13
14
  innerHTML: msg,
14
15
  className: 'tip'
15
16
  })
@@ -1,6 +1,7 @@
1
1
  import anime from 'theme-shokax-anime'
2
2
  import { siteNavHeight } from '../globals/globalVars'
3
3
  import type { AnimeOptions } from 'theme-shokax-anime/dist/types'
4
+ import {getTop, setDisplay} from './proto'
4
5
 
5
6
  /**
6
7
  * 参数 动画效果
@@ -26,7 +27,7 @@ export const transition = (target: HTMLElement, type: number|string|Function, co
26
27
  case 'bounceUpIn':
27
28
  animation = {
28
29
  begin (anim) {
29
- target.display('block')
30
+ setDisplay(target, 'block')
30
31
  },
31
32
  translateY: [
32
33
  { value: -60, duration: 200 },
@@ -41,7 +42,7 @@ export const transition = (target: HTMLElement, type: number|string|Function, co
41
42
  case 'shrinkIn':
42
43
  animation = {
43
44
  begin (anim) {
44
- target.display('block')
45
+ setDisplay(target, 'block')
45
46
  },
46
47
  scale: [
47
48
  { value: 1.1, duration: 300 },
@@ -54,7 +55,7 @@ export const transition = (target: HTMLElement, type: number|string|Function, co
54
55
  case 'slideRightIn':
55
56
  animation = {
56
57
  begin (anim) {
57
- target.display('block')
58
+ setDisplay(target, 'block')
58
59
  },
59
60
  translateX: ['100%', '0%'],
60
61
  opacity: [0, 1]
@@ -82,7 +83,7 @@ export const transition = (target: HTMLElement, type: number|string|Function, co
82
83
  begin && begin()
83
84
  },
84
85
  complete () {
85
- target.display(display)
86
+ setDisplay(target, display)
86
87
  complete && complete()
87
88
  }
88
89
  }, animation)).play()
@@ -100,7 +101,7 @@ export const pageScroll = (target: HTMLElement|number, offset?: number, complete
100
101
  // 动画缓动函数
101
102
  easing: 'easeInOutQuad',
102
103
  // 如果 offset 存在,则滚动到 offset,如果 target 是数字,则滚动到 target,如果 target 是 DOM 元素,则滚动到下述表达式
103
- scrollTop: offset || (typeof target === 'number' ? target : (target ? target.getTop() + document.documentElement.scrollTop - siteNavHeight : 0)),
104
+ scrollTop: offset || (typeof target === 'number' ? target : (target ? getTop(target) + document.documentElement.scrollTop - siteNavHeight : 0)),
104
105
  // 完成回调函数
105
106
  complete () {
106
107
  complete && complete()
@@ -27,7 +27,10 @@ declare interface EventTarget {
27
27
  hasClass(className: string): boolean;
28
28
  }
29
29
 
30
+ type walineMeta = 'nick'|'mail'|'link'
31
+
30
32
  declare const LOCAL: {
33
+ ispost: boolean;
31
34
  path: string;
32
35
  ignores: Array<(uri:string)=>boolean>;
33
36
  audio: string[];
@@ -53,7 +56,7 @@ declare const LOCAL: {
53
56
  show: string
54
57
  }
55
58
  }
56
- declare const CONFIG: {
59
+ interface configType {
57
60
  hostname: string;
58
61
  fireworks: any;
59
62
  audio: AudioItem[];
@@ -92,20 +95,29 @@ declare const CONFIG: {
92
95
  fancybox: string
93
96
  }
94
97
  search: any,
95
- valine: string
98
+ waline: {
99
+ serverURL: string
100
+ lang: string
101
+ locale: object
102
+ emoji: boolean
103
+ meta: walineMeta[]
104
+ requiredMeta: walineMeta[]
105
+ wordLimit: number
106
+ pageSize: number
107
+ pageview: boolean
108
+ }
109
+ twikoo: {
110
+ envId: string
111
+ region: string
112
+ }
113
+ walinePageView: boolean
96
114
  quicklink: {
97
115
  ignores: any
98
116
  timeout: number
99
- priority: string
117
+ priority: boolean
100
118
  }
101
119
  playerAPI: string
102
120
  }
103
- declare const instantsearch: any
104
-
105
- declare function algoliasearch(appID: string, apiKey: string): any;
106
-
107
- declare const quicklink: any
108
-
109
121
  // esbuild 静态常量
110
122
  declare const __shokax_player__:boolean
111
123
  declare const __shokax_fireworks__:boolean
@@ -115,3 +127,7 @@ declare const __shokax_outime__:boolean
115
127
  declare const __shokax_tabs__: boolean
116
128
  declare const __shokax_quiz__: boolean
117
129
  declare const __shokax_fancybox__: boolean
130
+ declare const __shokax_waline__:boolean
131
+ declare const __shokax_twikoo__:boolean
132
+ declare const shokax_CONFIG:configType
133
+ declare const shokax_siteURL:string
@@ -1,4 +1,6 @@
1
1
  import { getScript } from './scriptPjax'
2
+ import { CONFIG } from '../globals/globalVars'
3
+ import { createChild } from './proto'
2
4
 
3
5
  /**
4
6
  * 用途是根据不同的资源名称和类型生成相应的资源 URL。
@@ -31,7 +33,7 @@ export const vendorCss = (type: string, condition?: string): void => {
31
33
  }
32
34
 
33
35
  if (LOCAL[type]) {
34
- document.head.createChild('link', {
36
+ createChild(document.head, 'link', {
35
37
  rel: 'stylesheet',
36
38
  href: assetUrl('css', type)
37
39
  })
@@ -1,84 +1,82 @@
1
1
  import { $dom } from './dom'
2
2
 
3
+ export const insertAfter = function (el: HTMLElement, element: HTMLElement): void {
4
+ const parent = el.parentNode
5
+ if (parent.lastChild === el) {
6
+ parent.appendChild(element)
7
+ } else {
8
+ parent.insertBefore(element, el.nextSibling)
9
+ }
10
+ }
11
+
12
+ /**
13
+ * 创建一个子节点并放置
14
+ */
15
+ export const createChild = function (parent: HTMLElement, tag: string, obj: object, positon?: string): HTMLElement {
16
+ const child = document.createElement(tag)
17
+ Object.assign(child, obj)
18
+ switch (positon) {
19
+ case 'after':
20
+ insertAfter(parent, child)
21
+ break
22
+ case 'replace':
23
+ parent.innerHTML = ''
24
+ parent.appendChild(child)
25
+ break
26
+ default:
27
+ parent.appendChild(child)
28
+ }
29
+ return child
30
+ }
31
+
32
+ /**
33
+ * 此方法使用`<div>`包装一个 DOM 元素
34
+ * @param parent
35
+ * @param obj 需要被包装的对象
36
+ */
37
+ export const wrapObject = function (parent: HTMLElement, obj: any): void {
38
+ const box = document.createElement('div')
39
+ Object.assign(box, obj)
40
+ parent.parentNode.insertBefore(box, parent)
41
+ parent.parentNode.removeChild(parent)
42
+ box.appendChild(parent)
43
+ }
44
+
45
+ export const getHeight = function (el: HTMLElement): number {
46
+ return el.getBoundingClientRect().height
47
+ }
48
+
49
+ export const setWidth = function (el: HTMLElement, w: number|string): void {
50
+ el.style.width = typeof w === 'number' ? w + 'rem' : w
51
+ }
52
+
53
+ export const getWidth = function (el: HTMLElement): number {
54
+ return el.getBoundingClientRect().width
55
+ }
56
+
57
+ export const getTop = function (el: HTMLElement): number {
58
+ return el.getBoundingClientRect().top
59
+ }
60
+
61
+ export const getLeft = function (el: HTMLElement): number {
62
+ return el.getBoundingClientRect().left
63
+ }
64
+
65
+ export const getDisplay = function (el: HTMLElement): string {
66
+ return el.style.display
67
+ }
68
+
69
+ export const setDisplay = function (el: HTMLElement, d: string): HTMLElement {
70
+ el.style.display = d
71
+ return el
72
+ }
73
+
74
+ // TODO 未完成迁移
75
+ export const child = function (el: HTMLElement, selector: string): HTMLElement {
76
+ return $dom(selector, (el as unknown as Document))
77
+ }
3
78
  export default function initProto () {
4
79
  Object.assign(HTMLElement.prototype, {
5
- /**
6
- * 创建一个子节点并放置
7
- */
8
- createChild (tag: string, obj: ElementCreationOptions, positon?: string): HTMLElement {
9
- const child = document.createElement(tag)
10
- Object.assign(child, obj)
11
- switch (positon) {
12
- case 'after':
13
- this.insertAfter(child)
14
- break
15
- case 'replace':
16
- this.innerHTML = ''
17
- this.appendChild(child)
18
- break
19
- default:
20
- this.appendChild(child)
21
- }
22
- return child
23
- },
24
- /**
25
- * 此方法使用`<div>`包装一个 DOM 元素
26
- * @param obj 需要被包装的对象
27
- */
28
- wrapObject (obj: HTMLElement) {
29
- const box = document.createElement('div')
30
- Object.assign(box, obj)
31
- this.parentNode.insertBefore(box, this)
32
- this.parentNode.removeChild(this)
33
- box.appendChild(this)
34
- },
35
- changeOrGetHeight (h?: number | string): number {
36
- if (h) {
37
- // TODO 0rem是期望的值吗?
38
- this.style.height = typeof h === 'number' ? h + 'rem' : h
39
- }
40
- return this.getBoundingClientRect().height
41
- },
42
- /**
43
- 此函数将元素的宽度设置为指定值,如果未提供值,则返回元素的宽度.<br />
44
- 宽度可以作为数字提供(假定它以`rem`为单位).作为字符串提供则直接设置为元素宽度
45
- */
46
- changeOrGetWidth (w?: number | string): number {
47
- if (w) {
48
- // TODO 0rem是期望的值吗?
49
- this.style.width = typeof w === 'number' ? w + 'rem' : w
50
- }
51
- return this.getBoundingClientRect().width
52
- },
53
- getTop (): number {
54
- return this.getBoundingClientRect().top
55
- },
56
- left (): number {
57
- return this.getBoundingClientRect().left
58
- },
59
- /**
60
- * 将此节点插入父节点的下一个节点之前
61
- */
62
- insertAfter (element: HTMLElement): void {
63
- const parent = this.parentNode
64
- if (parent.lastChild === this) {
65
- parent.appendChild(element)
66
- } else {
67
- parent.insertBefore(element, this.nextSibling)
68
- }
69
- },
70
- /**
71
- * 当d为空时返回此节点的CSSStyle display属性 <br />
72
- * 反之,将d设置为此节点的CSSStyle display属性
73
- */
74
- display (d?: string): string | EventTarget {
75
- if (d == null) {
76
- return this.style.display
77
- } else {
78
- this.style.display = d
79
- return this
80
- }
81
- },
82
80
  /**
83
81
  * 找到此节点第一个符合selector选择器的子节点
84
82
  */