Merge pull request #5393 from vinothkannans/clip-master
FEATURE: Paste plain text table as Markdown table
This commit is contained in:
commit
e14281bdf6
|
@ -396,10 +396,10 @@ export default Ember.Component.extend({
|
||||||
});
|
});
|
||||||
|
|
||||||
$element.on('fileuploadpaste', (e) => {
|
$element.on('fileuploadpaste', (e) => {
|
||||||
let {types} = clipboardData(e);
|
const clipboard = clipboardData(e);
|
||||||
this._pasted = true;
|
this._pasted = true;
|
||||||
|
|
||||||
if (types.some(t => t === "text/plain")) {
|
if (clipboard.types.some(t => t === "text/plain")) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -616,10 +616,43 @@ export default Ember.Component.extend({
|
||||||
Ember.run.scheduleOnce("afterRender", () => $textarea.focus());
|
Ember.run.scheduleOnce("afterRender", () => $textarea.focus());
|
||||||
},
|
},
|
||||||
|
|
||||||
paste(e) {
|
_detectTable(text) {
|
||||||
let {types} = clipboardData(e);
|
if (text.endsWith("\n")) {
|
||||||
|
text = text.substring(0, text.length - 1);
|
||||||
|
let rows = text.split("\r").join("").split("\n");
|
||||||
|
|
||||||
if (types.some(t => t === "Files") && !types.some(t => t === "text/plain")) {
|
if (rows.length > 1) {
|
||||||
|
const columns = rows.map(r => r.split("\t").length);
|
||||||
|
const isTable = columns.reduce((a, b) => a && columns[0] === b && b > 1);
|
||||||
|
|
||||||
|
if (isTable) {
|
||||||
|
const splitterRow = [...Array(columns[0])].map(c => "---").join("\t");
|
||||||
|
rows.splice(1, 0, splitterRow);
|
||||||
|
|
||||||
|
return "|" + rows.map(r => r.split("\t").join("|")).join("|\n|") + "|\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
|
||||||
|
paste(e) {
|
||||||
|
const clipboard = clipboardData(e);
|
||||||
|
const types = clipboard.types;
|
||||||
|
let preventDefault = false;
|
||||||
|
|
||||||
|
if (types.some(t => t === "text/plain")) {
|
||||||
|
const text = clipboard.getData("text/plain");
|
||||||
|
const table = this._detectTable(text);
|
||||||
|
if (table) {
|
||||||
|
this._addText(this._getSelected(), table);
|
||||||
|
preventDefault = true;
|
||||||
|
}
|
||||||
|
} else if (types.some(t => t === "Files")) {
|
||||||
|
preventDefault = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preventDefault) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -422,12 +422,10 @@ export function isAppleDevice() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function clipboardData(e) {
|
export function clipboardData(e) {
|
||||||
let data = e.clipboardData ||
|
return e.clipboardData ||
|
||||||
e.originalEvent.clipboardData ||
|
e.originalEvent.clipboardData ||
|
||||||
e.delegatedEvent.originalEvent.clipboardData ||
|
e.delegatedEvent.originalEvent.clipboardData ||
|
||||||
event.clipboardData;
|
event.clipboardData;
|
||||||
|
|
||||||
return { items: data.items, types: data.types };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This prevents a mini racer crash
|
// This prevents a mini racer crash
|
||||||
|
|
Loading…
Reference in New Issue