Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
55382a10
V
vscode
项目概览
掘金者说
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
55382a10
编写于
12月 18, 2017
作者:
M
Matt Bierner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cache code-line elements for md scroll-sync
Fixes #19092
上级
dcaee0a1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
26 deletion
+48
-26
extensions/markdown/media/main.js
extensions/markdown/media/main.js
+48
-26
未找到文件。
extensions/markdown/media/main.js
浏览文件 @
55382a10
...
...
@@ -29,6 +29,10 @@
};
}
/**
* @param {string} command
* @param {any[]} args
*/
function
postMessage
(
command
,
args
)
{
window
.
parent
.
postMessage
({
command
:
'
did-click-link
'
,
...
...
@@ -36,24 +40,47 @@
},
'
file://
'
);
}
/**
* @typedef {{ element: Element, line: number }} CodeLineElement
*/
/**
* @return {CodeLineElement[]}
*/
const
getCodeLineElements
=
(()
=>
{
/** @type {CodeLineElement[]} */
let
elements
;
return
()
=>
{
if
(
!
elements
)
{
elements
=
Array
.
prototype
.
map
.
call
(
document
.
getElementsByClassName
(
'
code-line
'
),
element
=>
{
const
line
=
+
element
.
getAttribute
(
'
data-line
'
);
return
{
element
,
line
}
})
.
filter
(
x
=>
!
isNaN
(
x
.
line
));
}
return
elements
;
};
})()
/**
* Find the html elements that map to a specific target line in the editor.
*
* If an exact match, returns a single element. If the line is between elements,
* returns the element prior to and the element after the given line.
*
* @param {number} targetLine
*
* @returns {{ previous: CodeLineElement, next?: CodeLineElement }}
*/
function
getElementsForSourceLine
(
targetLine
)
{
const
lines
=
document
.
getElementsByClassName
(
'
code-line
'
);
let
previous
=
lines
[
0
]
&&
+
lines
[
0
].
getAttribute
(
'
data-line
'
)
?
{
line
:
+
lines
[
0
].
getAttribute
(
'
data-line
'
),
element
:
lines
[
0
]
}
:
null
;
for
(
const
element
of
lines
)
{
const
lineNumber
=
+
element
.
getAttribute
(
'
data-line
'
);
if
(
isNaN
(
lineNumber
))
{
continue
;
}
const
entry
=
{
line
:
lineNumber
,
element
:
element
};
if
(
lineNumber
===
targetLine
)
{
const
lines
=
getCodeLineElements
();
let
previous
=
lines
[
0
]
||
null
;
for
(
const
entry
of
lines
)
{
if
(
entry
.
line
===
targetLine
)
{
return
{
previous
:
entry
,
next
:
null
};
}
else
if
(
lineNumber
>
targetLine
)
{
}
else
if
(
entry
.
line
>
targetLine
)
{
return
{
previous
,
next
:
entry
};
}
previous
=
entry
;
...
...
@@ -64,15 +91,10 @@
/**
* Find the html elements that are at a specific pixel offset on the page.
*
* @returns {{ previous:
{ element: any, line: number }, next?: { element: any, line: number }
}}
* @returns {{ previous:
CodeLineElement, next?: CodeLineElement
}}
*/
function
getLineElementsAtPageOffset
(
offset
)
{
const
allLines
=
document
.
getElementsByClassName
(
'
code-line
'
);
/** @type {Element[]} */
const
lines
=
Array
.
prototype
.
filter
.
call
(
allLines
,
element
=>
{
const
line
=
+
element
.
getAttribute
(
'
data-line
'
);
return
!
isNaN
(
line
)
});
const
lines
=
getCodeLineElements
()
const
position
=
offset
-
window
.
scrollY
;
...
...
@@ -80,7 +102,7 @@
let
hi
=
lines
.
length
-
1
;
while
(
lo
+
1
<
hi
)
{
const
mid
=
Math
.
floor
((
lo
+
hi
)
/
2
);
const
bounds
=
lines
[
mid
].
getBoundingClientRect
();
const
bounds
=
lines
[
mid
].
element
.
getBoundingClientRect
();
if
(
bounds
.
top
+
bounds
.
height
>=
position
)
{
hi
=
mid
;
}
else
{
...
...
@@ -89,18 +111,16 @@
}
const
hiElement
=
lines
[
hi
];
const
hiLine
=
+
hiElement
.
getAttribute
(
'
data-line
'
);
if
(
hi
>=
1
&&
hiElement
.
getBoundingClientRect
().
top
>
position
)
{
if
(
hi
>=
1
&&
hiElement
.
element
.
getBoundingClientRect
().
top
>
position
)
{
const
loElement
=
lines
[
lo
];
const
loLine
=
+
loElement
.
getAttribute
(
'
data-line
'
);
const
bounds
=
loElement
.
getBoundingClientRect
();
const
previous
=
{
element
:
loElement
,
line
:
loLine
+
(
position
-
bounds
.
top
)
/
(
bounds
.
height
)
};
const
next
=
{
element
:
hiElement
,
line
:
hiLine
,
fractional
:
0
};
const
bounds
=
loElement
.
element
.
getBoundingClientRect
();
const
previous
=
{
element
:
loElement
.
element
,
line
:
loElement
.
line
+
(
position
-
bounds
.
top
)
/
(
bounds
.
height
)
};
const
next
=
{
element
:
hiElement
.
element
,
line
:
hiElement
.
line
,
fractional
:
0
};
return
{
previous
,
next
};
}
const
bounds
=
hiElement
.
getBoundingClientRect
();
const
previous
=
{
element
:
hiElement
,
line
:
hiL
ine
+
(
position
-
bounds
.
top
)
/
(
bounds
.
height
)
};
const
bounds
=
hiElement
.
element
.
getBoundingClientRect
();
const
previous
=
{
element
:
hiElement
.
element
,
line
:
hiElement
.
l
ine
+
(
position
-
bounds
.
top
)
/
(
bounds
.
height
)
};
return
{
previous
};
}
...
...
@@ -110,6 +130,8 @@
/**
* Attempt to reveal the element for a source line in the editor.
*
* @param {number} line
*/
function
scrollToRevealSourceLine
(
line
)
{
const
{
previous
,
next
}
=
getElementsForSourceLine
(
line
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录