Skip to content

Commit 9de5042

Browse files
authored
Add a metadata tag to override notebook direction (ltr/rtl) (#5052)
* Add a metadata tag to control notebook/cell direction (ltr/rtl) * Fix test errors
1 parent 356966a commit 9de5042

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

notebook/static/base/js/dialog.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ define(['jquery',
230230
return false;
231231
}
232232
options.callback(new_md);
233+
options.notebook.apply_directionality();
233234
}
234235
}
235236
},

notebook/static/notebook/js/actions.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,29 @@ define([
6565
*
6666
**/
6767
var _actions = {
68+
'toggle-cell-rtl-layout': {
69+
cmd: i18n.msg._('toggle current cell ltr/rtl direction'),
70+
help: i18n.msg._('Toggle current cell directionality between left-to-right and right-to-left'),
71+
handler: function (env) {
72+
var notebook_direction = document.body.getAttribute('dir') == 'rtl' ? 'rtl' : 'ltr';
73+
var current_cell_default_direction = env.notebook.get_selected_cell().cell_type == 'code' ? 'ltr' : notebook_direction;
74+
var current_cell_direction = env.notebook.get_selected_cell().metadata.direction || current_cell_default_direction;
75+
var new_direction = current_cell_direction == 'rtl' ? 'ltr' : 'rtl';
76+
env.notebook.get_selected_cells().forEach(
77+
function(cell) { cell.metadata.direction = new_direction; }
78+
);
79+
env.notebook.set_dirty(true);
80+
env.notebook.apply_directionality();
81+
}
82+
},
6883
'toggle-rtl-layout': {
69-
cmd: i18n.msg._('toggle rtl layout'),
70-
help: i18n.msg._('Toggle the screen directionality between left-to-right and right-to-left'),
71-
handler: function () {
72-
(document.body.getAttribute('dir')=='rtl') ? document.body.setAttribute('dir','ltr') : document.body.setAttribute('dir','rtl');
84+
cmd: i18n.msg._('toggle notebook ltr/rtl direction'),
85+
help: i18n.msg._('Toggle notebook directionality between left-to-right and right-to-left'),
86+
handler: function (env) {
87+
var new_direction = document.body.getAttribute('dir') == 'rtl' ? 'ltr' : 'rtl';
88+
env.notebook.metadata.direction = new_direction;
89+
env.notebook.set_dirty(true);
90+
env.notebook.apply_directionality();
7391
}
7492
},
7593
'edit-command-mode-keyboard-shortcuts': {

notebook/static/notebook/js/notebook.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,27 @@ define([
690690
this.line_numbers = !this.line_numbers;
691691
};
692692

693+
/**
694+
* Reads direction settings (LTR/RTL) from notebook/cells metadata
695+
* and applies them to display elements.
696+
*/
697+
Notebook.prototype.apply_directionality = function () {
698+
var notebook_direction = this.metadata.direction || 'ltr';
699+
// html
700+
document.body.setAttribute('dir', notebook_direction);
701+
// existing cells
702+
this.get_cells().forEach( function(cell) {
703+
if (cell.cell_type == 'markdown') {
704+
cell.code_mirror.setOption('direction', cell.metadata.direction || notebook_direction);
705+
cell.element.find('.rendered_html').attr('dir', cell.metadata.direction || notebook_direction);
706+
} else if (cell.cell_type == 'code') {
707+
cell.element.find('.output_text').attr('dir', cell.metadata.direction || 'auto');
708+
}
709+
});
710+
// new cells
711+
textcell.MarkdownCell.options_default.cm_config.direction = notebook_direction;
712+
};
713+
693714
/**
694715
* Get the cell above a given cell.
695716
*
@@ -2662,6 +2683,9 @@ define([
26622683
this.trusted = trusted;
26632684
this.events.trigger("trust_changed.Notebook", trusted);
26642685
}
2686+
2687+
this.apply_directionality();
2688+
26652689
};
26662690

26672691
/**

notebook/static/notebook/js/textcell.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ define([
303303
MarkdownCell.options_default = {
304304
cm_config: {
305305
mode: 'ipythongfm',
306-
direction: bidi.isMirroringEnabled() ? 'rtl' : 'ltr'
307306
},
308307
placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$"
309308
};

notebook/static/notebook/less/outputarea.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,7 @@ div.output_unrecognized {
211211
}
212212
}
213213
}
214+
215+
div.output_text[dir="rtl"] {
216+
text-align: right;
217+
}

notebook/static/notebook/less/renderedhtml.less

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@
131131
* + .alert {margin-top: 1em;}
132132
}
133133

134-
[dir="rtl"] .rendered_html {
134+
// Right align text iff:
135+
// (a) notebook is rtl and it's not overriden in cell metadata (i.e. rtl or none)
136+
// (b) notebook is whatever but cell metadata specifies rtl
137+
// note that cell metadata is used to set 'dir' attribute of rendered_html element.
138+
[dir="rtl"] .rendered_html:not([dir="ltr"]), .rendered_html[dir="rtl"] {
135139
p {
136140
text-align : right;
137141
}

0 commit comments

Comments
 (0)