Skip to content

Commit

Permalink
fix: 后台插入图集、表情后页面位置错误
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanDecode committed Apr 8, 2019
1 parent fa3d437 commit 3213213
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
53 changes: 33 additions & 20 deletions assets/editor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
/* eslint-disable linebreak-style */
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */

function insertAtCursor(myField, myValue) {
var textTop = myField.scrollTop;
var documentTop = document.documentElement.scrollTop;

//IE 浏览器
if (document.selection) {
myField.focus();
var sel = document.selection.createRange();
sel.text = myValue;
sel.select();
}

//FireFox、Chrome等
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
myField.focus();
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
} else {
myField.value += myValue;
myField.focus();
}

myField.scrollTop = textTop;
document.documentElement.scrollTop=documentTop;
}

$(function() {
if($('#wmd-button-row').length>0){
$('#wmd-button-row').append('<li class="wmd-spacer wmd-spacer1"></li><li class="wmd-button" id="wmd-photoset-button" style="" title="插入图集">图集</li>');
Expand All @@ -15,25 +47,6 @@ $(function() {
}
$(document).on('click','#wmd-photoset-button',function() {
myField = document.getElementById('text');
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = '\n\n[photos]\n\n'+sel.text+'[/photos]\n\n';
myField.focus();
}
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ '\n\n[photos]\n\n[/photos]\n\n'
+ myField.value.substring(endPos, myField.value.length);
myField.focus();
myField.selectionStart=startPos+'[photos]'.length+3;
myField.selectionEnd=startPos+'[photos]'.length+3;
}
else{
myField.value +='\n\n[photos]\n\n[/photos]\n\n';
myField.focus();
}
insertAtCursor(myField, '\n\n[photos]\n\n[/photos]\n\n');
});
});
43 changes: 41 additions & 2 deletions assets/libs/owo/owo_01.js → assets/libs/owo/owo_02.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
/* eslint-disable linebreak-style */
/* eslint-disable no-console */
/* eslint-disable no-undef */
/* eslint-disable quotes */
/* eslint-disable no-unused-vars */
'use strict';

function insertAtCursor(myField, myValue) {
var textTop = myField.scrollTop;
var documentTop = document.documentElement.scrollTop;

//IE 浏览器
if (document.selection) {
myField.focus();
var sel = document.selection.createRange();
sel.text = myValue;
sel.select();
}

//FireFox、Chrome等
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;

myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);

myField.focus();
myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
} else {

myField.value += myValue;
myField.focus();
}

myField.scrollTop = textTop;
document.documentElement.scrollTop=documentTop;
}

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
Expand Down Expand Up @@ -100,9 +137,11 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var cursorPos = _this2.area.selectionEnd;
var areaValue = _this2.area.value;
if (target.dataset.id == "not-given") {
_this2.area.value = areaValue.slice(0, cursorPos) + target.innerHTML + areaValue.slice(cursorPos) + ' ';
insertAtCursor(_this2.area, ' ' + target.innerHTML + ' ');
//_this2.area.value = areaValue.slice(0, cursorPos) + target.innerHTML + areaValue.slice(cursorPos) + ' ';
} else {
_this2.area.value = areaValue.slice(0, cursorPos) + target.dataset.id + areaValue.slice(cursorPos) + ' ';
insertAtCursor(_this2.area, ' ' + target.dataset.id + ' ');
//_this2.area.value = areaValue.slice(0, cursorPos) + target.dataset.id + areaValue.slice(cursorPos) + ' ';
}
_this2.area.focus();
_this2.toggle();
Expand Down
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable linebreak-style */
/* eslint-disable no-undef */
var gulp = require('gulp');
var sass = require('gulp-sass');
Expand Down
2 changes: 1 addition & 1 deletion libs/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function isMobile()
public static function addButton()
{
echo '<script src="';
self::indexTheme('/assets/libs/owo/owo_01.js');
self::indexTheme('/assets/libs/owo/owo_02.js');
echo '"></script>';

echo '<script src="';
Expand Down

0 comments on commit 3213213

Please sign in to comment.