@discuzq/sdk
Version:
discuz dsk
88 lines (67 loc) • 2.59 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
require("core-js/modules/es.array.concat.js");
require("core-js/modules/es.array.slice.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.regexp.exec.js");
require("core-js/modules/es.regexp.to-string.js");
require("core-js/modules/es.string.replace.js");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.h5Share = h5Share;
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
/**
* h5和pc复制链接分享
* @param {ShareInfo | string} shareInfo 如果是string就当作标题
* @ShareInfo
* @title {string} 标题
* @path {string} 分享的路由会使用内部host
* @url {string} 分享的url会替代内部合成的url
* @return {boolean} 复制是否成功
* @example h5Share({title:"dzq123", path:"/home/post?ddd=123"}) 复制 -> `dzq123 ${host}/home/post?ddd=123`
*/
function h5Share(shareInfo) {
var themeTitle = '';
var path = '';
var url = ''; // title处理
switch ((0, _typeof2.default)(shareInfo)) {
case 'string':
themeTitle = shareInfo;
break;
default:
shareInfo = shareInfo || {}; // path处理
path = shareInfo.path || '';
if (path && /^\/.*/.test(path)) {
// 处理开头的/
path = path.slice(1);
}
themeTitle = shareInfo.title;
}
themeTitle = themeTitle || 'Discuz!Q';
var reTag = /<img(?:.|\s)*?>/g;
var reTag2 = /(<\/?br.*?>)/gi;
var reTag3 = /(<\/?p.*?>)/gi;
themeTitle = themeTitle.toString().replace(reTag, '');
themeTitle = themeTitle.toString().replace(reTag2, '');
themeTitle = themeTitle.toString().replace(reTag3, '');
themeTitle = themeTitle.toString().replace(/\s+/g, '');
themeTitle = "".concat(themeTitle.substring(0, 17)); // url合并
var DISCUZ_REQUEST_HOST = window && window.location && window.location.origin || '';
url = shareInfo.url || "".concat(DISCUZ_REQUEST_HOST, "/").concat(path); // 复制流程
var oInput = document.createElement('input');
oInput.value = url;
document.body.appendChild(oInput);
oInput.select(); // 选择对象
oInput.readOnly = true;
oInput.id = 'copyInp'; // 执行浏览器复制命令
var copyState = document.execCommand('Copy');
oInput.setAttribute('onfocus', undefined);
oInput.blur();
oInput.className = 'oInput';
oInput.style.display = 'none';
return copyState;
}
var _default = h5Share;
exports.default = _default;