"""Offline Playwright Chromium verification lab for jEasyUI Basic Drag & Drop (Ch07)."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jEasyUI Basic Drag & Drop Lab</title>
<style>
body { margin: 0; padding: 0; font-family: Arial, sans-serif; }
.dd-demo { position: absolute; width: 60px; height: 60px; border: 2px solid #95b8e7; background: #e0ecff; text-align: center; line-height: 60px; user-select: none; cursor: move; }
#dd1 { left: 20px; top: 20px; }
#dd2 { left: 120px; top: 20px; }
#dd3 { left: 220px; top: 20px; }
.proxy { position: absolute; width: 60px; height: 60px; border: 2px dashed #d9534f; background: #fff3cd; text-align: center; line-height: 60px; pointer-events: none; }
</style>
</head>
<body>
<div id="dd1" class="dd-demo">dd1</div>
<div id="dd2" class="dd-demo">dd2</div>
<div id="dd3" class="dd-demo">dd3</div>
<script>
window.__dragLog = [];
function $(arg) {
if (typeof arg === 'string' && arg.trim().startsWith('<')) {
const tmp = document.createElement('div');
tmp.innerHTML = arg.trim();
const el = tmp.firstElementChild;
return {
_el: el,
appendTo(target) {
document.querySelector(target).appendChild(el);
return this;
}
};
}
const el = typeof arg === 'string' ? document.querySelector(arg) : arg;
return {
_el: el,
draggable(opts) {
const cfg = Object.assign({ proxy: null }, opts || {});
el.addEventListener('mousedown', (e) => {
const startX = e.clientX;
const startY = e.clientY;
const rect = el.getBoundingClientRect();
const origLeft = rect.left;
const origTop = rect.top;
let proxyEl = null;
if (cfg.proxy === 'clone') {
proxyEl = el.cloneNode(true);
proxyEl.removeAttribute('id');
proxyEl.classList.add('dd-clone-proxy');
proxyEl.style.left = origLeft + 'px';
proxyEl.style.top = origTop + 'px';
proxyEl.style.opacity = '0.7';
document.body.appendChild(proxyEl);
} else if (typeof cfg.proxy === 'function') {
const res = cfg.proxy(el);
proxyEl = res._el || res;
proxyEl.style.left = origLeft + 'px';
proxyEl.style.top = origTop + 'px';
}
const targetEl = proxyEl || el;
function onMove(ev) {
const dx = ev.clientX - startX;
const dy = ev.clientY - startY;
targetEl.style.left = (origLeft + dx) + 'px';
targetEl.style.top = (origTop + dy) + 'px';
}
function onUp(ev) {
document.removeEventListener('mousemove', onMove);
document.removeEventListener('mouseup', onUp);
const dx = ev.clientX - startX;
const dy = ev.clientY - startY;
el.style.left = (origLeft + dx) + 'px';
el.style.top = (origTop + dy) + 'px';
if (proxyEl) proxyEl.remove();
}
document.addEventListener('mousemove', onMove);
document.addEventListener('mouseup', onUp);
});
return this;
}
};
}
window.$ = $;
$('#dd1').draggable();
$('#dd2').draggable({ proxy: 'clone' });
$('#dd3').draggable({
proxy: function(source) {
var p = $('<div class="proxy">proxy</div>');
p.appendTo('body');
return p;
}
});
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: Default draggable (#dd1) moves directly without proxy
page.mouse.move(50, 50)
page.mouse.down()
page.mouse.move(130, 100)
mid1 = page.evaluate("""() => {
const r = document.getElementById('dd1').getBoundingClientRect();
return { midLeft: Math.round(r.left), midTop: Math.round(r.top), proxyCount: document.querySelectorAll('.dd-clone-proxy,.proxy').length };
}""")
page.mouse.up()
end1 = page.evaluate("""() => {
const r = document.getElementById('dd1').getBoundingClientRect();
return { endLeft: Math.round(r.left), endTop: Math.round(r.top) };
}""")
act1 = dict(mid1, **end1)
exp1 = {"midLeft": 100, "midTop": 70, "proxyCount": 0, "endLeft": 100, "endTop": 70}
cases.append({
"name": "default_direct_drag",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
})
# Case 2: Clone proxy draggable (#dd2) creates clone during drag and cleans up on drop
page.mouse.move(150, 50)
page.mouse.down()
page.mouse.move(210, 110)
mid2 = page.evaluate("""() => {
const src = document.getElementById('dd2').getBoundingClientRect();
const prx = document.querySelector('.dd-clone-proxy');
const pr = prx.getBoundingClientRect();
return {
sourceStayedLeft: Math.round(src.left),
cloneExists: Boolean(prx),
cloneLeft: Math.round(pr.left),
cloneTop: Math.round(pr.top)
};
}""")
page.mouse.up()
end2 = page.evaluate("""() => {
const src = document.getElementById('dd2').getBoundingClientRect();
return {
afterDropLeft: Math.round(src.left),
afterDropTop: Math.round(src.top),
cloneCleanedUp: document.querySelectorAll('.dd-clone-proxy').length === 0
};
}""")
act2 = dict(mid2, **end2)
exp2 = {
"sourceStayedLeft": 120,
"cloneExists": True,
"cloneLeft": 180,
"cloneTop": 80,
"afterDropLeft": 180,
"afterDropTop": 80,
"cloneCleanedUp": True,
}
cases.append({
"name": "clone_proxy_drag",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
})
# Case 3: Custom proxy function (#dd3) renders <div class="proxy">proxy</div>
page.mouse.move(250, 50)
page.mouse.down()
page.mouse.move(300, 120)
mid3 = page.evaluate("""() => {
const prx = document.querySelector('body > .proxy');
const pr = prx.getBoundingClientRect();
return {
proxyText: prx.textContent,
proxyLeft: Math.round(pr.left),
proxyTop: Math.round(pr.top)
};
}""")
page.mouse.up()
end3 = page.evaluate("""() => {
const src = document.getElementById('dd3').getBoundingClientRect();
return {
afterDropLeft: Math.round(src.left),
afterDropTop: Math.round(src.top),
proxyCleanedUp: document.querySelectorAll('.proxy').length === 0
};
}""")
act3 = dict(mid3, **end3)
exp3 = {
"proxyText": "proxy",
"proxyLeft": 270,
"proxyTop": 90,
"afterDropLeft": 270,
"afterDropTop": 90,
"proxyCleanedUp": True,
}
cases.append({
"name": "custom_proxy_function_drag",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
})
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/jeasyui-dd-basic-lab/explore-dd-basic.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-dd-basic.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a990.zip
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip