Skip to content

Commit

Permalink
FIX: 解决粘贴的 table col 宽度为 NaN% 的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
biallo committed Jan 17, 2018
1 parent e2beff2 commit 03bbe1d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 63 deletions.
38 changes: 15 additions & 23 deletions lib/simditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2329,23 +2329,11 @@ Clipboard = (function(superClass) {
};

Clipboard.prototype._processPasteContent = function(pasteContent) {
var $blockEl, $img, blob, children, dataURLtoBlob, img, insertPosition, isTitle, k, l, lastLine, len, len1, len2, len3, len4, line, lines, m, node, o, q, ref, ref1, ref2, uploadOpt, uploader;
var $blockEl, $img, blob, children, dataURLtoBlob, img, insertPosition, k, l, lastLine, len, len1, len2, len3, len4, line, lines, m, node, o, q, ref, ref1, ref2, uploadOpt, uploader;
if (this.editor.triggerHandler('pasting', [pasteContent]) === false) {
return;
}
$blockEl = this._pasteInBlockEl;
isTitle = (function() {
var i, titles;
titles = ['h1', 'h2', 'h3', 'h4', 'h5'];
i = 0;
while (i < titles.length) {
if ($blockEl.is(titles[i])) {
return true;
}
i++;
}
return false;
})();
if (!pasteContent) {
return;
} else if (this._pastePlainText) {
Expand Down Expand Up @@ -2376,7 +2364,7 @@ Clipboard = (function(superClass) {
} else if (pasteContent.length === 1) {
if (pasteContent.is('p')) {
children = pasteContent.contents();
if (isTitle) {
if ($blockEl.is('h1, h2, h3, h4, h5')) {
if (children.length) {
children.css('font-size', '');
}
Expand Down Expand Up @@ -5012,7 +5000,7 @@ TableButton = (function(superClass) {
$.merge(this.editor.formatter._allowedTags, ['thead', 'th', 'tbody', 'tr', 'td', 'colgroup', 'col']);
$.extend(this.editor.formatter._allowedAttributes, {
td: ['rowspan', 'colspan'],
col: ['width']
col: ['style']
});
$.extend(this.editor.formatter._allowedStyles, {
td: ['text-align'],
Expand Down Expand Up @@ -5330,14 +5318,18 @@ TableButton = (function(superClass) {
};

TableButton.prototype.refreshTableWidth = function($table) {
var cols, tableWidth;
tableWidth = $table.width();
cols = $table.find('col');
return $table.find('thead tr th').each(function(i, td) {
var $col;
$col = cols.eq(i);
return $col.attr('width', ($(td).outerWidth() / tableWidth * 100) + '%');
});
return setTimeout((function(_this) {
return function() {
var cols, tableWidth;
tableWidth = $table.width();
cols = $table.find('col');
return $table.find('thead tr th').each(function(i, td) {
var $col;
$col = cols.eq(i);
return $col.attr('width', ($(td).outerWidth() / tableWidth * 100) + '%');
});
};
})(this), 0);
};

TableButton.prototype.setActive = function(active) {
Expand Down
38 changes: 15 additions & 23 deletions site/assets/scripts/simditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2329,23 +2329,11 @@ Clipboard = (function(superClass) {
};

Clipboard.prototype._processPasteContent = function(pasteContent) {
var $blockEl, $img, blob, children, dataURLtoBlob, img, insertPosition, isTitle, k, l, lastLine, len, len1, len2, len3, len4, line, lines, m, node, o, q, ref, ref1, ref2, uploadOpt, uploader;
var $blockEl, $img, blob, children, dataURLtoBlob, img, insertPosition, k, l, lastLine, len, len1, len2, len3, len4, line, lines, m, node, o, q, ref, ref1, ref2, uploadOpt, uploader;
if (this.editor.triggerHandler('pasting', [pasteContent]) === false) {
return;
}
$blockEl = this._pasteInBlockEl;
isTitle = (function() {
var i, titles;
titles = ['h1', 'h2', 'h3', 'h4', 'h5'];
i = 0;
while (i < titles.length) {
if ($blockEl.is(titles[i])) {
return true;
}
i++;
}
return false;
})();
if (!pasteContent) {
return;
} else if (this._pastePlainText) {
Expand Down Expand Up @@ -2376,7 +2364,7 @@ Clipboard = (function(superClass) {
} else if (pasteContent.length === 1) {
if (pasteContent.is('p')) {
children = pasteContent.contents();
if (isTitle) {
if ($blockEl.is('h1, h2, h3, h4, h5')) {
if (children.length) {
children.css('font-size', '');
}
Expand Down Expand Up @@ -5012,7 +5000,7 @@ TableButton = (function(superClass) {
$.merge(this.editor.formatter._allowedTags, ['thead', 'th', 'tbody', 'tr', 'td', 'colgroup', 'col']);
$.extend(this.editor.formatter._allowedAttributes, {
td: ['rowspan', 'colspan'],
col: ['width']
col: ['style']
});
$.extend(this.editor.formatter._allowedStyles, {
td: ['text-align'],
Expand Down Expand Up @@ -5330,14 +5318,18 @@ TableButton = (function(superClass) {
};

TableButton.prototype.refreshTableWidth = function($table) {
var cols, tableWidth;
tableWidth = $table.width();
cols = $table.find('col');
return $table.find('thead tr th').each(function(i, td) {
var $col;
$col = cols.eq(i);
return $col.attr('width', ($(td).outerWidth() / tableWidth * 100) + '%');
});
return setTimeout((function(_this) {
return function() {
var cols, tableWidth;
tableWidth = $table.width();
cols = $table.find('col');
return $table.find('thead tr th').each(function(i, td) {
var $col;
$col = cols.eq(i);
return $col.attr('width', ($(td).outerWidth() / tableWidth * 100) + '%');
});
};
})(this), 0);
};

TableButton.prototype.setActive = function(active) {
Expand Down
15 changes: 9 additions & 6 deletions src/buttons/table.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TableButton extends Button
)
$.extend @editor.formatter._allowedAttributes,
td: ['rowspan', 'colspan'],
col: ['width']
col: ['style']
$.extend @editor.formatter._allowedStyles,
td: ['text-align']
th: ['text-align']
Expand Down Expand Up @@ -330,11 +330,14 @@ class TableButton extends Button
$table

refreshTableWidth: ($table)->
tableWidth = $table.width()
cols = $table.find('col')
$table.find('thead tr th').each (i, td) ->
$col = cols.eq(i)
$col.attr 'width', ($(td).outerWidth() / tableWidth * 100) + '%'
# 解决无法在第一时间获取粘贴 table 宽度的问题
setTimeout =>
tableWidth = $table.width()
cols = $table.find('col')
$table.find('thead tr th').each (i, td) ->
$col = cols.eq(i)
$col.attr 'width', ($(td).outerWidth() / tableWidth * 100) + '%'
, 0

setActive: (active) ->
super active
Expand Down
14 changes: 3 additions & 11 deletions src/clipboard.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Clipboard extends SimpleModule
@_pasteInBlockEl = @editor.selection.blockNodes().last()
@_pastePlainText = @opts.cleanPaste || @_pasteInBlockEl.is('pre, table')


if @_pastePlainText
pasteContent = @editor.formatter.clearHtml @_pasteBin.html(), true
else
Expand All @@ -107,15 +108,6 @@ class Clipboard extends SimpleModule
return if @editor.triggerHandler('pasting', [pasteContent]) == false
$blockEl = @_pasteInBlockEl

isTitle = do ->
titles = ['h1', 'h2', 'h3', 'h4', 'h5']
i = 0
while i < titles.length
if $blockEl.is(titles[i])
return true
i++
false

if !pasteContent
return
else if @_pastePlainText
Expand All @@ -134,11 +126,11 @@ class Clipboard extends SimpleModule
@editor.selection.insertNode(node) for node in pasteContent
else if pasteContent.length < 1
return
else if pasteContent.length == 1
else if pasteContent.length == 1
if pasteContent.is('p')
children = pasteContent.contents()

if isTitle
if $blockEl.is 'h1, h2, h3, h4, h5'
if children.length
children.css('font-size', '')

Expand Down

0 comments on commit 03bbe1d

Please sign in to comment.