1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-12 21:30:39 +00:00

fix-selection

This commit is contained in:
Michael18811380328
2018-12-13 14:30:45 +08:00
parent 0d629a4f8e
commit ccb2008282
2 changed files with 38 additions and 6 deletions

View File

@@ -32,6 +32,6 @@
} }
.review-comment-dialog .review-comment-dialog-quote { .review-comment-dialog .review-comment-dialog-quote {
margin-top: 10px; margin-top: 10px;
max-height: 4rem; max-height: 6rem;
overflow: hidden; overflow: auto;
} }

View File

@@ -21,7 +21,7 @@ import { findRange } from '@seafile/slate-react';
import { Nav, NavItem, NavLink, TabContent, TabPane } from 'reactstrap'; import { Nav, NavItem, NavLink, TabContent, TabPane } from 'reactstrap';
import classnames from 'classnames'; import classnames from 'classnames';
import HistoryList from './pages/review/history-list'; import HistoryList from './pages/review/history-list';
import { Value } from 'slate'; import { Value, Document, Block } from 'slate';
import './assets/css/fa-solid.css'; import './assets/css/fa-solid.css';
import './assets/css/fa-regular.css'; import './assets/css/fa-regular.css';
@@ -58,6 +58,7 @@ class DraftReview extends React.Component {
this.newIndex = null; this.newIndex = null;
this.oldIndex = null; this.oldIndex = null;
this.changeIndex = -1; this.changeIndex = -1;
this.range = null;
} }
componentDidMount() { componentDidMount() {
@@ -210,14 +211,17 @@ class DraftReview extends React.Component {
setBtnPosition = (e) => { setBtnPosition = (e) => {
const nativeSelection = window.getSelection(); const nativeSelection = window.getSelection();
if (!nativeSelection.rangeCount) { if (!nativeSelection.rangeCount) {
this.range = null;
return; return;
} }
if (nativeSelection.isCollapsed === false) { if (nativeSelection.isCollapsed === false) {
const nativeRange = nativeSelection.getRangeAt(0); const nativeRange = nativeSelection.getRangeAt(0);
let range = findRange(nativeRange, this.refs.diffViewer.value); let range = findRange(nativeRange, this.refs.diffViewer.value);
if (!range) { if (!range) {
this.range = null;
return; return;
} }
this.range = range;
let rect = nativeRange.getBoundingClientRect(); let rect = nativeRange.getBoundingClientRect();
// fix Safari bug // fix Safari bug
if (navigator.userAgent.indexOf('Chrome') < 0 && navigator.userAgent.indexOf('Safari') > 0) { if (navigator.userAgent.indexOf('Chrome') < 0 && navigator.userAgent.indexOf('Safari') > 0) {
@@ -237,7 +241,6 @@ class DraftReview extends React.Component {
} }
let style = this.refs.commentbtn.style; let style = this.refs.commentbtn.style;
style.top = `${rect.top - 100 + this.refs.viewContent.scrollTop}px`; style.top = `${rect.top - 100 + this.refs.viewContent.scrollTop}px`;
return range;
} }
else { else {
let style = this.refs.commentbtn.style; let style = this.refs.commentbtn.style;
@@ -247,7 +250,7 @@ class DraftReview extends React.Component {
getQuote = () => { getQuote = () => {
this.quote = ''; this.quote = '';
let range = this.setBtnPosition(); let range = this.range;
if (!range) { if (!range) {
return; return;
} }
@@ -280,8 +283,12 @@ class DraftReview extends React.Component {
} }
} }
let fragment = document.getFragmentAtRange(range); let fragment = document.getFragmentAtRange(range);
let nodes = this.removeNullNode(fragment.nodes);
let newFragment = Document.create({
nodes: nodes
});
let newValue = Value.create({ let newValue = Value.create({
document: fragment document: newFragment
}); });
this.quote = serialize(newValue.toJSON()); this.quote = serialize(newValue.toJSON());
let blockPath = document.createSelection(range).anchor.path.slice(0, 1); let blockPath = document.createSelection(range).anchor.path.slice(0, 1);
@@ -290,6 +297,31 @@ class DraftReview extends React.Component {
this.oldIndex = node.data.get('old_index'); this.oldIndex = node.data.get('old_index');
} }
removeNullNode = (oldNodes) => {
let newNodes = [];
oldNodes.map((node) => {
const text = node.text.trim();
const childNodes = node.nodes;
if (!text) {
return;
}
if ((childNodes && childNodes.size === 1) || (!childNodes)) {
newNodes.push(node);
}
else if (childNodes.size > 1) {
let nodes = this.removeNullNode(childNodes);
let newNode = Block.create({
nodes: nodes,
data: node.data,
key: node.key,
type: node.type
});
newNodes.push(newNode);
}
});
return newNodes;
}
addComment = (e) => { addComment = (e) => {
e.stopPropagation(); e.stopPropagation();
this.getQuote(); this.getQuote();