Last active
June 3, 2022 01:54
-
-
Save fghpdf/73b7790dd6d72a81a850967f8af99de8 to your computer and use it in GitHub Desktop.
webworker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var importer = require('richdoc-xlsx'); | |
self.onmessage = function (file) { | |
var reader; | |
reader = new FileReader(); | |
reader.readAsArrayBuffer(file); | |
reader.onload = function (e) { | |
var bufferData, result; | |
bufferData = new Buffer(new Uint8Array(reader.result)); | |
console.log('=start import'); | |
result = importer.import(bufferData); | |
console.log('=got result', result.length); | |
self.postMessage(result); | |
}; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
controllers.files.importSpreadsheet = (req, res) -> | |
console.log('====1') | |
fileName = req.body.name | |
unless fileName | |
fileName = if req.files.files.name then req.files.files.name.replace(/\.xlsx$/, '') else '导入的表格' | |
console.log('====2') | |
doc = req.body.delta | |
doc = doc.replace(/"/g, "\"") | |
unless doc | |
spreadsheetFile = req.files.files.path | |
if spreadsheetFile.match /\.xls$/ | |
spreadsheetFile = yield convertFormat(spreadsheetFile) | |
console.log('===3') | |
fileContent = fs.readFileSync(spreadsheetFile) | |
console.log('===4', fileContent.length) | |
imported = richdocXLSX.import(fileContent) | |
console.log('===5') | |
doc = richdoc.pack(imported) | |
console.log('===6') | |
file = yield fileHelper.createNewFile({ | |
name: fileName | |
parent_id: req.param('parent'), | |
user_id: req.session.user.id, | |
type: -1, | |
content: doc | |
}) | |
res.send({ guid: file.guid }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define ['common', 'richdoc'], (common, richdoc) -> | |
exports = {} | |
exports.supportedTypes = | |
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | |
doc: 'application/msword' | |
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | |
xls: 'application/vnd.ms-excel' | |
exports.import = (file) -> | |
newText = '' | |
# 清除 input, 否则 onchange 不会触发 | |
clearForm = () -> | |
inputFile = $('input#import-file') | |
inputFile.replaceWith(inputFile = inputFile.val('').clone()) | |
fd = new FormData() | |
fd.append('files', file) | |
fd.append('parent', cow.currentFile.id || 0) | |
# common.alert(title: "正在导入…") | |
fileType = file.name.split('.').pop() | |
if _.includes(['xls', 'xlsx'], fileType) | |
# $.ajax | |
# url: '/import/spreadsheet' | |
# type: 'POST' | |
# data: fd | |
# cache: false | |
# contentType: false | |
# processData: false | |
# success: (result) -> | |
# if result.code isnt undefined | |
# common.alert(title: "导入出错,请重试。", type: 'error') | |
# else | |
# page("/spreadsheet/#{result.guid}") | |
# clearForm() | |
common.alert(title: "正在导入表格,处理大文件耗时较长,请耐心等候…") | |
require.ensure([], () -> | |
importer = require('richdoc-xlsx') | |
# reader = new FileReader() | |
# supportRABS = reader.readAsBinaryString | |
# reader.readAsArrayBuffer(file) | |
# # reader[if supportRABS then 'readAsBinaryString' else 'readAsArrayBuffer'](file) | |
# reader.onload = (e) -> | |
# bufferData = new Buffer(new Uint8Array(reader.result)) | |
# importer = require('richdoc-xlsx') | |
# console.log('=start import') | |
# result = richdoc.pack(importer.import(bufferData)) | |
# console.log('=got result', result.length) | |
worker = new Worker('/static/client/assets/scripts/import_file_reader.bundle.js') | |
console.log('worker has been build') | |
worker.postMessage(file) | |
worker.onerror = (event) -> | |
console.log(event.filename, event.lineno, event.message) | |
worker.onmessage = (message) -> | |
result = richdoc.pack(message) | |
$.ajax | |
url: '/import/spreadsheet' | |
type: 'POST' | |
data: { name: file.name, delta: result } | |
success: (result) -> | |
if result.code isnt undefined | |
common.alert(title: "导入出错,请重试。", type: 'error') | |
else | |
page("/spreadsheet/#{result.guid}") | |
clearForm() | |
, 'export-excel') | |
else | |
common.alert(title: "正在导入…") | |
require.ensure([], () -> | |
contentCollector = require('content_collector') | |
Changeset = require('changeset') | |
AttributePool = require('attribute_pool') | |
eachAttribRun = (attribs, func) -> | |
attribsIter = Changeset.opIterator(attribs) | |
textIndex = 0 | |
newTextStart = 0 | |
newTextEnd = newText.length | |
while attribsIter.hasNext() | |
op = attribsIter.next() | |
nextIndex = textIndex + op.chars | |
if !(nextIndex <= newTextStart or textIndex >= newTextEnd) | |
func Math.max(newTextStart, textIndex), Math.min(newTextEnd, nextIndex), op.attribs | |
textIndex = nextIndex | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment