Skip to content

Instantly share code, notes, and snippets.

@9wick
Created May 4, 2018 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9wick/e9d43b111ddacbfdd244d39a30dda0ce to your computer and use it in GitHub Desktop.
Save 9wick/e9d43b111ddacbfdd244d39a30dda0ce to your computer and use it in GitHub Desktop.
codemirrorのlint error/warningをモバイルでも表示する ref: https://qiita.com/wicket/items/709c9be0e73b868d85c2
CodeMirror.on(cm.display.scroller, "touchend", function (e) {
let touch = cm.display.activeTouch;
let date = new Date;
if (touch && touch.left != null &&
!touch.moved && date - touch.start < 300) {
//エディタの何処かをsingle tapした
if (!touch.prev) {
//行に変換するならこれ
var pos = cm.coordsChar(touch, "page");
//gutterの範囲判定
if(touch.left < Math.floor(cm.display.gutters.getBoundingClientRect().right)){
showLintTooltip(pos.line);
return;
}
}
}
//hide tooltip
if(currentTooptipTarget) {
dispatchMouseEvent(currentTooptipTarget, "mouseout", 0, 0);
currentTooptipTarget = null;
}
});
let currentTooptipTarget = null ;
function showLintTooltip(line){
let markers = jsEditor.state.lint.marked;
let info = jsEditor.lineInfo(line);
for (let marker of markers) {
if (marker.lines[0].lineNo() === line) {
//need to tooltip
let element = info.gutterMarkers["CodeMirror-lint-markers"];
if (element) {
let rect = element.getBoundingClientRect();
console.log(rect);
if(currentTooptipTarget == element){
//hide tooltip
dispatchMouseEvent(element, "mouseout", rect.x, rect.y);
currentTooptipTarget = null;
}else {
//hide tooltip
if(currentTooptipTarget) {
dispatchMouseEvent(currentTooptipTarget, "mouseout", rect.x, rect.y);
}
//show tooltip
dispatchMouseEvent(element, "mouseover", rect.left+rect.width/2, rect.top+rect.height/2);
currentTooptipTarget = element;
}
return;
}
}
}
}
function dispatchMouseEvent(elm, type,x,y){
var mouseMoveEvent = document.createEvent("MouseEvents");
mouseMoveEvent.initMouseEvent(
type, //event type : click, mousedown, mouseup, mouseover, mousemove, mouseout.
true, //canBubble
false, //cancelable
window, //event's AbstractView : should be window
1, // detail : Event's mouse click count
x, // screenX
y, // screenY
x, // clientX
y, // clientY
false, // ctrlKey
false, // altKey
false, // shiftKey
false, // metaKey
0, // button : 0 = click, 1 = middle button, 2 = right button
null // relatedTarget : Only used with some event types (e.g. mouseover and mouseout). In other cases, pass null.
);
elm.dispatchEvent(mouseMoveEvent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment