Skip to content

Commit 8a58af0

Browse files
committed
lock scope height after clear scope in use_scope(clear=True)`
1 parent c712981 commit 8a58af0

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

docs/spec.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ The ``spec`` fields of ``output_ctl`` commands:
367367
- null: Do nothing
368368
- `'remove'`: Remove the old scope first and then create a new one
369369
- `'clear'`: Just clear the contents of the old scope, but don’t create a new scope
370+
- `'blank'`: Clear the contents of the old scope and keep the height, don’t create a new scope
370371

372+
* loose: css selector of the scope, set the scope not to keep the height (i.e., revoke the effect of ``set_scope(if_exist='blank')``)
371373
* clear: css selector of the scope need to clear
372374
* clear_before
373375
* clear_after

pywebio/output.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,8 +1765,8 @@ def app():
17651765

17661766
def before_enter():
17671767
if create_scope:
1768-
if_exist = 'clear' if clear else None
1769-
set_scope(name, if_exist=if_exist, **scope_params)
1768+
if_exist = 'blank' if clear else None
1769+
set_scope(name, if_exist=if_exist, **scope_params) # lock the height of the scope and clear its content
17701770

17711771
return use_scope_(name=name, before_enter=before_enter)
17721772

@@ -1787,7 +1787,8 @@ def __exit__(self, exc_type, exc_val, exc_tb):
17871787
If this method returns True, it means that the context manager can handle the exception,
17881788
so that the with statement terminates the propagation of the exception
17891789
"""
1790-
get_current_session().pop_scope()
1790+
scope = get_current_session().pop_scope()
1791+
send_msg('output_ctl', dict(loose=scope2dom(scope))) # revoke lock the height of the scope
17911792
return False # Propagate Exception
17921793

17931794
def __call__(self, func):

webiojs/src/handlers/output.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const DISPLAY_NONE_TAGS = ['script', 'style'];
1010
let after_show_callbacks: (() => void) [] = [];
1111

1212
// register a callback to execute after the current output widget showing
13-
export function AfterCurrentOutputWidgetShow(callback: () => void){
13+
export function AfterCurrentOutputWidgetShow(callback: () => void) {
1414
after_show_callbacks.push(callback);
1515
}
1616

@@ -86,7 +86,7 @@ export class OutputHandler implements CommandHandler {
8686

8787
// to avoid widget width exceeding page width
8888
// show horizon scroll bar when content too wide
89-
if(elem.width() > this.container_elem.width())
89+
if (elem.width() > this.container_elem.width())
9090
elem.wrap($(document.createElement('div')).css('overflow', 'auto'));
9191

9292
if (this.is_elem_visible(elem) && container_elem.length == 1) { // 输出内容为可见标签且输出目的scope唯一
@@ -127,11 +127,20 @@ export class OutputHandler implements CommandHandler {
127127
else if (spec.if_exist == 'clear') {
128128
old.empty();
129129
return;
130+
} else if (spec.if_exist == 'blank') { // Clear the contents of the old scope and keep the height
131+
let scope_css: any = {'min-height': old.height()};
132+
let prev = old.prev(), next = old.next();
133+
if (prev.length)
134+
scope_css['margin-top'] = old[0].getBoundingClientRect().top - prev[0].getBoundingClientRect().bottom;
135+
if (next.length)
136+
scope_css['margin-bottom'] = next[0].getBoundingClientRect().top - old[0].getBoundingClientRect().bottom;
137+
old.css(scope_css);
138+
old.empty();
139+
return;
130140
} else {
131141
return;
132142
}
133143
}
134-
135144
let html = `<div id="${spec.set_scope}"></div>`;
136145
if (spec.position === 0)
137146
container_elem.prepend(html);
@@ -144,6 +153,9 @@ export class OutputHandler implements CommandHandler {
144153
$(`${spec.container} > *`).eq(spec.position).insertAfter(html);
145154
}
146155
}
156+
if (msg.spec.loose !== undefined) { // revoke the effect of ``set_scope(if_exist='blank')``
157+
$(msg.spec.loose).css({'min-height': '', 'margin-top': '', 'margin-bottom': ''});
158+
}
147159
if (msg.spec.clear !== undefined) {
148160
$(msg.spec.clear).empty();
149161
}

0 commit comments

Comments
 (0)