Просмотр исходного кода

Merge pull request #2075 from weseek/imprv/refactor-user-picture

Imprv/refactor user picture
Yuki Takei 6 лет назад
Родитель
Сommit
ac1b681cbb

+ 3 - 2
src/client/js/components/Navbar/PageCreator.jsx

@@ -1,8 +1,9 @@
 import React from 'react';
 import PropTypes from 'prop-types';
 
+import { userPageRoot } from '@commons/util/path-utils';
+
 import UserPicture from '../User/UserPicture';
-import { userPageRoot } from '../../../../lib/util/path-utils';
 
 const PageCreator = (props) => {
   const { creator, createdAt, isCompactMode } = props;
@@ -13,7 +14,7 @@ const PageCreator = (props) => {
 
   return (
     <div className="d-flex align-items-center">
-      <div className="mr-2" href={userPageRoot(creator)} data-toggle="tooltip" data-placement="bottom" title={creator.name}>
+      <div className="mr-2">
         <UserPicture user={creator} size={pictureSize} />
       </div>
       {creatInfo}

+ 1 - 1
src/client/js/components/Navbar/PersonalDropdown.jsx

@@ -59,7 +59,7 @@ const PersonalDropdown = (props) => {
       {/* remove .dropdown-toggle for hide caret */}
       {/* See https://stackoverflow.com/a/44577512/13183572 */}
       <a className="nav-link waves-effect waves-light" data-toggle="dropdown">
-        <UserPicture user={user} withoutLink /><span className="d-none d-sm-inline-block">&nbsp;{user.name}</span>
+        <UserPicture user={user} withoutLink withoutTooltip /><span className="d-none d-sm-inline-block">&nbsp;{user.name}</span>
       </a>
 
       {/* Menu */}

+ 3 - 2
src/client/js/components/Navbar/RevisionAuthor.jsx

@@ -1,8 +1,9 @@
 import React from 'react';
 import PropTypes from 'prop-types';
 
+import { userPageRoot } from '@commons/util/path-utils';
+
 import UserPicture from '../User/UserPicture';
-import { userPageRoot } from '../../../../lib/util/path-utils';
 
 const RevisionAuthor = (props) => {
   const { revisionAuthor, updatedAt, isCompactMode } = props;
@@ -13,7 +14,7 @@ const RevisionAuthor = (props) => {
 
   return (
     <div className="d-flex align-items-center">
-      <div className="mr-2" href={userPageRoot(revisionAuthor)} data-toggle="tooltip" data-placement="bottom" title={revisionAuthor.name}>
+      <div className="mr-2">
         <UserPicture user={revisionAuthor} size={pictureSize} />
       </div>
       {updateInfo}

+ 26 - 10
src/client/js/components/User/UserPicture.jsx

@@ -2,6 +2,8 @@ import React from 'react';
 import md5 from 'md5';
 import PropTypes from 'prop-types';
 
+import { userPageRoot } from '@commons/util/path-utils';
+
 const DEFAULT_IMAGE = '/images/icons/user.svg';
 
 // TODO UserComponent?
@@ -49,6 +51,17 @@ export default class UserPicture extends React.Component {
     );
   }
 
+  RootElmWithoutLink = (props) => {
+    return <span {...props}>{props.children}</span>;
+  }
+
+  RootElmWithLink = (props) => {
+    const { user } = this.props;
+    const href = userPageRoot(user);
+
+    return <a href={href} {...props}>{props.children}</a>;
+  }
+
   render() {
     const user = this.props.user;
 
@@ -56,18 +69,18 @@ export default class UserPicture extends React.Component {
       return this.renderForNull();
     }
 
-    const imgElem = (
-      <img
-        src={this.getUserPicture(user)}
-        alt={user.username}
-        className={this.getClassName()}
-      />
-    );
+    const { withoutLink } = this.props;
+    const RootElm = withoutLink ? this.RootElmWithoutLink : this.RootElmWithLink;
+    const title = `@${user.username}<br />${user.name}`;
 
     return (
-      (this.props.withoutLink)
-        ? <span>{imgElem}</span>
-        : <a href={`/user/${user.username}`}>{imgElem}</a>
+      <RootElm data-toggle="tooltip" data-placement="bottom" data-html="true" title={title}>
+        <img
+          src={this.getUserPicture(user)}
+          alt={user.username}
+          className={this.getClassName()}
+        />
+      </RootElm>
     );
   }
 
@@ -77,8 +90,11 @@ UserPicture.propTypes = {
   user: PropTypes.object,
   size: PropTypes.string,
   withoutLink: PropTypes.bool,
+  withoutTooltip: PropTypes.bool,
 };
 
 UserPicture.defaultProps = {
   size: null,
+  withoutLink: false,
+  withoutTooltip: false,
 };

+ 6 - 24
src/client/js/components/User/UserPictureList.jsx

@@ -1,6 +1,6 @@
 import React from 'react';
 import PropTypes from 'prop-types';
-import { UncontrolledTooltip } from 'reactstrap';
+
 import { createSubscribedElement } from '../UnstatedUtils';
 import AppContainer from '../../services/AppContainer';
 
@@ -24,30 +24,12 @@ class UserPictureList extends React.Component {
 
   }
 
-  renderPictAndTooltip(user) {
-    const userId = user._id;
-    const userPictureContainerId = `userPictureContainer-${userId}`;
-
-    return (
-      <span key={userId}>
-        <span id={userPictureContainerId}>
-          <UserPicture user={user} size="xs" />
-        </span>
-        <UncontrolledTooltip
-          key={`tooltip-${userId}`}
-          placement="bottom"
-          target={userPictureContainerId}
-        >
-          @{user.username}<br />{user.name}
-        </UncontrolledTooltip>
-      </span>
-    );
-  }
-
   render() {
-    return this.state.users.map((user) => {
-      return this.renderPictAndTooltip(user);
-    });
+    return this.state.users.map(user => (
+      <span key={user._id}>
+        <UserPicture user={user} size="xs" />
+      </span>
+    ));
   }
 
 }

+ 1 - 1
src/server/views/widget/page_list.html

@@ -10,7 +10,7 @@
 <li>
   <img src="{{ page.lastUpdateUser|picture }}" class="picture rounded-circle">
   <a href="{{ page.path }}"
-    class="page-list-link text-break"
+    class="text-break"
     data-path="{{ page.path }}">{{ decodeURIComponent(page.path) }}
   </a>
   <span class="page-list-meta">