1
0
mirror of synced 2025-12-13 02:11:25 +08:00

feat(sources): use luna text viewer

This commit is contained in:
redhoodsu
2022-12-24 14:35:21 +08:00
parent 03d4e68756
commit f90bb1354f
9 changed files with 116 additions and 110 deletions

View File

@@ -1,3 +1 @@
test/util.js test/util.js
eruda.js
eruda.min.js

View File

@@ -66,6 +66,7 @@ module.exports = {
path.resolve(__dirname, '../node_modules/luna-data-grid'), path.resolve(__dirname, '../node_modules/luna-data-grid'),
path.resolve(__dirname, '../node_modules/luna-object-viewer'), path.resolve(__dirname, '../node_modules/luna-object-viewer'),
path.resolve(__dirname, '../node_modules/luna-dom-viewer'), path.resolve(__dirname, '../node_modules/luna-dom-viewer'),
path.resolve(__dirname, '../node_modules/luna-text-viewer'),
], ],
use: [ use: [
{ {

View File

@@ -70,6 +70,7 @@
"luna-notification": "^0.1.4", "luna-notification": "^0.1.4",
"luna-object-viewer": "^0.2.4", "luna-object-viewer": "^0.2.4",
"luna-tab": "^0.1.2", "luna-tab": "^0.1.2",
"luna-text-viewer": "^0.1.0",
"node-sass": "^7.0.1", "node-sass": "^7.0.1",
"postcss-clean": "^1.1.0", "postcss-clean": "^1.1.0",
"postcss-loader": "^3.0.0", "postcss-loader": "^3.0.0",

View File

@@ -6,6 +6,7 @@ import escape from 'licia/escape'
import copy from 'licia/copy' import copy from 'licia/copy'
import isJson from 'licia/isJson' import isJson from 'licia/isJson'
import Emitter from 'licia/Emitter' import Emitter from 'licia/Emitter'
import truncate from 'licia/truncate'
import { classPrefix as c } from '../lib/util' import { classPrefix as c } from '../lib/util'
export default class Detail extends Emitter { export default class Detail extends Emitter {
@@ -55,7 +56,11 @@ export default class Detail extends Emitter {
let resTxt = '' let resTxt = ''
if (data.resTxt) { if (data.resTxt) {
resTxt = `<pre class="${c('response')}">${escape(data.resTxt)}</pre>` let text = data.resTxt
if (text.length > MAX_RES_LEN) {
text = truncate(text, MAX_RES_LEN)
}
resTxt = `<pre class="${c('response')}">${escape(text)}</pre>`
} }
const html = `<div class="${c('control')}"> const html = `<div class="${c('control')}">
@@ -153,3 +158,5 @@ export default class Detail extends Emitter {
} }
} }
} }
const MAX_RES_LEN = 100000

View File

@@ -4,9 +4,9 @@ import Settings from '../Settings/Settings'
import ajax from 'licia/ajax' import ajax from 'licia/ajax'
import isStr from 'licia/isStr' import isStr from 'licia/isStr'
import escape from 'licia/escape' import escape from 'licia/escape'
import trim from 'licia/trim' import truncate from 'licia/truncate'
import map from 'licia/map'
import highlight from 'licia/highlight' import highlight from 'licia/highlight'
import LunaTextViewer from 'luna-text-viewer'
import evalCss from '../lib/evalCss' import evalCss from '../lib/evalCss'
import { classPrefix as c } from '../lib/util' import { classPrefix as c } from '../lib/util'
@@ -172,9 +172,15 @@ export default class Sources extends Tool {
_renderCode() { _renderCode() {
const data = this._data const data = this._data
this._renderHtml(`<div class="${c('code')}"></div>`, false)
let code = data.val let code = data.val
const len = data.val.length const len = data.val.length
if (len > MAX_RAW_LEN) {
code = truncate(code, MAX_RAW_LEN)
}
// If source code too big, don't process it. // If source code too big, don't process it.
if (len < MAX_BEAUTIFY_LEN) { if (len < MAX_BEAUTIFY_LEN) {
const curTheme = evalCss.getCurTheme() const curTheme = evalCss.getCurTheme()
@@ -189,44 +195,13 @@ export default class Sources extends Tool {
code = escape(code) code = escape(code)
} }
const showLineNum = len < MAX_LINE_NUM_LEN && this._showLineNum const container = this._$el.find(c('.code')).get(0)
new LunaTextViewer(container, {
if (showLineNum) { text: code,
code = code.split('\n').map((line, idx) => { escape: false,
if (trim(line) === '') line = '&nbsp;' wrapLongLines: true,
showLineNumbers: data.val.length < MAX_LINE_NUM_LEN && this._showLineNum,
return { })
idx: idx + 1,
val: line,
}
})
}
let html
if (showLineNum) {
const lineNum = map(code, ({ idx }) => {
return `<div class="${c('line-num')}">${idx}</div>`
}).join('')
const codeLine = map(code, ({ val }) => {
return `<pre class="${c('code-line')}">${val}</pre>`
}).join('')
html = `<div class="${c('code-wrapper')}">
<table class="${c('code')}">
<tbody>
<tr>
<td class="${c('gutter')}">${lineNum}</td>
<td class="${c('content')}">${codeLine}</td>
</tr>
</tbody>
</table>
</div>`
} else {
html = `<div class="${c('code-wrapper')}">
<pre class="${c('code')}">${code}</pre>
</div>`
}
this._renderHtml(html)
} }
_renderObj() { _renderObj() {
// Using cache will keep binding events to the same elements. // Using cache will keep binding events to the same elements.
@@ -251,9 +226,23 @@ export default class Sources extends Tool {
objViewer.set(val) objViewer.set(val)
} }
_renderRaw() { _renderRaw() {
const data = this._data
this._renderHtml(`<div class="${c('raw-wrapper')}"> this._renderHtml(`<div class="${c('raw-wrapper')}">
<div class="${c('raw')}">${escape(this._data.val)}</div> <div class="${c('raw')}"></div>
</div>`) </div>`)
let val = data.val
const container = this._$el.find(c('.raw')).get(0)
if (val.length > MAX_RAW_LEN) {
val = truncate(val, MAX_RAW_LEN)
}
new LunaTextViewer(container, {
text: val,
wrapLongLines: true,
showLineNumbers: val.length < MAX_LINE_NUM_LEN && this._showLineNum,
})
} }
_renderIframe() { _renderIframe() {
this._renderHtml(`<iframe src="${escape(this._data.val)}"></iframe>`) this._renderHtml(`<iframe src="${escape(this._data.val)}"></iframe>`)
@@ -267,5 +256,6 @@ export default class Sources extends Tool {
} }
} }
const MAX_BEAUTIFY_LEN = 100000 const MAX_BEAUTIFY_LEN = 30000
const MAX_LINE_NUM_LEN = 400000 const MAX_LINE_NUM_LEN = 80000
const MAX_RAW_LEN = 100000

View File

@@ -11,35 +11,9 @@
width: 100%; width: 100%;
min-height: 100%; min-height: 100%;
} }
.raw { .raw,
font-size: $font-size-s;
user-select: text;
padding: $padding;
}
.code { .code {
font-size: $font-size-s; height: 100%;
.content * {
user-select: text;
}
}
pre.code {
padding: $padding;
}
table.code {
border-collapse: collapse;
.gutter {
background: var(--background);
color: var(--primary);
}
.line-num {
border-right: 1px solid var(--border);
padding: 0 3px 0 5px;
text-align: right;
}
.code-line {
padding: 0 4px;
white-space: pre;
}
} }
.image { .image {
font-size: $font-size-s; font-size: $font-size-s;

View File

@@ -29,7 +29,13 @@ import evalCss from './lib/evalCss'
import chobitsu from './lib/chobitsu' import chobitsu from './lib/chobitsu'
export default { export default {
init({ container, tool, autoScale = true, useShadowDom = true, defaults = {} } = {}) { init({
container,
tool,
autoScale = true,
useShadowDom = true,
defaults = {},
} = {}) {
if (this._isInit) return if (this._isInit) return
this._isInit = true this._isInit = true
@@ -171,8 +177,13 @@ export default {
if (shadowRoot) { if (shadowRoot) {
// font-face doesn't work inside shadow dom. // font-face doesn't work inside shadow dom.
evalCss.container = document.head evalCss.container = document.head
evalCss(require('./style/icon.css') + evalCss(
require('luna-console/luna-console.css') + require('luna-object-viewer/luna-object-viewer.css') + require('luna-dom-viewer/luna-dom-viewer.css')) require('./style/icon.css') +
require('luna-console/luna-console.css') +
require('luna-object-viewer/luna-object-viewer.css') +
require('luna-dom-viewer/luna-dom-viewer.css') +
require('luna-text-viewer/luna-text-viewer.css')
)
el = document.createElement('div') el = document.createElement('div')
shadowRoot.appendChild(el) shadowRoot.appendChild(el)
@@ -181,13 +192,13 @@ export default {
} }
if (!this._shadowRoot) { if (!this._shadowRoot) {
el = document.createElement('div') el = document.createElement('div')
container.appendChild(el) container.appendChild(el)
} }
extend(el, { extend(el, {
className: 'eruda-container __chobitsu-hide__', className: 'eruda-container __chobitsu-hide__',
contentEditable: false contentEditable: false,
}) })
// http://stackoverflow.com/questions/3885018/active-pseudo-class-doesnt-work-in-mobile-safari // http://stackoverflow.com/questions/3885018/active-pseudo-class-doesnt-work-in-mobile-safari
@@ -197,7 +208,7 @@ export default {
}, },
_initDevTools(defaults) { _initDevTools(defaults) {
this._devTools = new DevTools(this._$el, { this._devTools = new DevTools(this._$el, {
defaults defaults,
}) })
}, },
_initStyle() { _initStyle() {
@@ -214,15 +225,16 @@ export default {
evalCss( evalCss(
require('./style/reset.scss') + require('./style/reset.scss') +
require('luna-object-viewer/luna-object-viewer.css') + require('luna-object-viewer/luna-object-viewer.css') +
require('luna-console/luna-console.css') + require('luna-console/luna-console.css') +
require('luna-notification/luna-notification.css') + require('luna-notification/luna-notification.css') +
require('luna-data-grid/luna-data-grid.css') + require('luna-data-grid/luna-data-grid.css') +
require('luna-dom-viewer/luna-dom-viewer.css') + require('luna-dom-viewer/luna-dom-viewer.css') +
require('luna-modal/luna-modal.css') + require('luna-modal/luna-modal.css') +
require('luna-tab/luna-tab.css') + require('luna-tab/luna-tab.css') +
require('./style/style.scss') + require('luna-text-viewer/luna-text-viewer.css') +
require('./style/icon.css') require('./style/style.scss') +
require('./style/icon.css')
) )
}, },
_initEntryBtn() { _initEntryBtn() {
@@ -246,14 +258,14 @@ export default {
'resources', 'resources',
'sources', 'sources',
'info', 'info',
'snippets' 'snippets',
] ]
) { ) {
tool = toArr(tool) tool = toArr(tool)
const devTools = this._devTools const devTools = this._devTools
tool.forEach(name => { tool.forEach((name) => {
const Tool = this[upperFirst(name)] const Tool = this[upperFirst(name)]
try { try {
if (Tool) devTools.add(new Tool()) if (Tool) devTools.add(new Tool())
@@ -269,5 +281,5 @@ export default {
}) })
devTools.showTool(tool[0] || 'settings') devTools.showTool(tool[0] || 'settings')
} },
} }

View File

@@ -332,3 +332,26 @@
.luna-tab-slider { .luna-tab-slider {
background: var(--accent); background: var(--accent);
} }
.luna-text-viewer {
color: var(--foreground);
border: none;
background: var(--background);
font-size: $font-size-s;
.luna-text-viewer-line-code {
user-select: text;
* {
user-select: text;
}
}
.luna-text-viewer-copy,
.luna-text-viewer-line-number {
border-color: var(--border);
}
.luna-text-viewer-copy .luna-text-viewer-icon-check {
color: var(--accent);
}
.luna-text-viewer-copy {
background-color: var(--background);
}
}

View File

@@ -1,12 +1,12 @@
describe('devTools', function() { describe('devTools', function () {
describe('init', function() { describe('init', function () {
it('destroy', function() { it('destroy', function () {
eruda.destroy() eruda.destroy()
expect($('#eruda')).toHaveLength(0) expect($('#eruda')).toHaveLength(0)
}) })
it('init', function() { it('init', function () {
let container = document.createElement('div') let container = document.createElement('div')
container.id = 'eruda' container.id = 'eruda'
document.body.appendChild(container) document.body.appendChild(container)
@@ -14,7 +14,7 @@ describe('devTools', function() {
eruda.init({ eruda.init({
container: container, container: container,
tool: [], tool: [],
useShadowDom: false useShadowDom: false,
}) })
let $eruda = $('#eruda') let $eruda = $('#eruda')
@@ -22,49 +22,49 @@ describe('devTools', function() {
}) })
}) })
describe('tool', function() { describe('tool', function () {
it('add', function() { it('add', function () {
eruda.add({ eruda.add({
name: 'test', name: 'test',
init: function($el) { init: function ($el) {
this._$el = $el this._$el = $el
$el.html('Test Plugin') $el.html('Test Plugin')
} },
}) })
expect($('.eruda-test')).toContainText('Test Plugin') expect($('.eruda-test')).toContainText('Test Plugin')
}) })
it('show', function() { it('show', function () {
let $tool = $('.eruda-test') let $tool = $('.eruda-test')
expect($tool).toBeHidden() expect($tool).toBeHidden()
eruda.show('test') eruda.show('test')
expect($tool).toHaveCss({ display: 'block' }) expect($tool).toHaveCss({ display: 'block' })
}) })
it('remove', function() { it('remove', function () {
eruda.remove('test') eruda.remove('test')
expect($('.eruda-test')).toHaveLength(0) expect($('.eruda-test')).toHaveLength(0)
}) })
}) })
describe('display', function() { describe('display', function () {
it('show', function() { it('show', function () {
eruda.show() eruda.show()
expect($('.eruda-dev-tools')).toHaveCss({ display: 'block' }) expect($('.eruda-dev-tools')).toHaveCss({ display: 'block' })
}) })
it('hide', function(done) { it('hide', function (done) {
eruda.hide() eruda.hide()
setTimeout(function() { setTimeout(function () {
expect($('.eruda-dev-tools')).toBeHidden() expect($('.eruda-dev-tools')).toBeHidden()
done() done()
}, 500) }, 500)
}) })
}) })
describe('scale', function() { describe('scale', function () {
it('get', function() { it('get', function () {
eruda.scale(1) eruda.scale(1)
expect(eruda.scale()).toBe(1) expect(eruda.scale()).toBe(1)
}) })