Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile, Desktop: Make table horizontally scrollable by give table a div parent, so the width of the table is limited to the screen width. #10161

Merged
merged 8 commits into from Mar 25, 2024

Conversation

wljince007
Copy link
Contributor

@wljince007 wljince007 commented Mar 20, 2024

Summary

Table adds div parent, so the width of the table is limited to the screen width. #8994 use jQuery or handy-scroll to implement scrollbar floating on Desktop end, introducing too much code, this PR does not dependent on jQuery or handy-scroll, do not modify table layout logic on desktop.

Mobile Screen

android test:

android_test_small.mp4

Ios test:

ios_test_small.mp4

desktop test:

desktop_test_small.mp4

Implementation Description

Add a markdown option in packages/lib/models/Setting.ts:

'markdown.plugin.tableHorizontallyScrollable': { storage: SettingStorage.File, isGlobal: true, value: false, type: SettingItemType.Bool, section: 'markdownPlugins', public: true, appTypes: [AppType.Mobile, AppType.Desktop], label: () => `${_('Enable table horizontally scrollable')}${wysiwygNo}` },

packages/renderer/MdToHtml/rules/tableHorizontallyScrollable.ts: make table adds div parent, make table horizontally scrollable.

<div class="joplin-table-div" >
      <table class="maps-to-line" source-line="186" source-line-end="312">
     .....
      </table>
</div>

…le is limited to the screen width on mobile devices
Copy link
Collaborator

@personalizedrefrigerator personalizedrefrigerator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this!

I've left a few comments below.

@@ -249,6 +250,10 @@ function shimInit() {
});
};

platformUtil.mobilePlatform = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware that shim-init-react.js isn't run within the mobile WebView where rendering happens. As such, platform.mobilePlatform() may return '' while rendering.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}

.joplin-table-div{
overflow-x: overlay;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
overflow-x: overlay;
overflow-x: auto;

It looks like overflow: overlay; is a legacy alias for overflow: auto — consider using overflow-x: auto here instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -58,6 +59,9 @@ export default function(theme: any, options: Options = null) {

const fontFamily = '\'Avenir\', \'Arial\', sans-serif';

const isMobile = platformUtil.isMobile();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an option to Options rather than moving platformUtil to utils. Because rendering now happens within a WebView (see e92f89d), after merging with dev, platformUtil.isMobile will return false in the renderer on mobile platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Comment on lines 399 to 402
.mce-content-body .joplin-editable {
cursor: pointer !important;
${overflowHidden}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think .mce-content-body should apply to the TinyMCE rich text editor. Consider removing this or adding a comment explaining why this style is applied here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}
const cur = String(self.renderToken(tokens, idx, options));
logger.info(`cur:${cur}`);
return platformUtil.isMobile() ? `<div class="joplin-table-div" data-handy-scroll>\n${cur}` : cur;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return platformUtil.isMobile() ? `<div class="joplin-table-div" data-handy-scroll>\n${cur}` : cur;
return platformUtil.isMobile() ? `<div class="joplin-table-div" >\n${cur}` : cur;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

tokens[idx].attrSet('source-line-end', `${lineEnd}`);
}
const cur = String(self.renderToken(tokens, idx, options));
logger.info(`cur:${cur}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing this and the other similar logger.info statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@wljince007
Copy link
Contributor Author

wljince007 commented Mar 21, 2024

@personalizedrefrigerator Thank you for patient guidance. I will try to add an option to control whether the table is restricted to div; I did not fully understand the design and multiple running environments of Joplin, and I will no longer modify shim.ts .

wljince007 and others added 4 commits March 21, 2024 09:34
overflow: overlay; is a legacy alias for overflow: auto

Co-authored-by: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com>
Remove joplin-table-div incorrect CSS attribute

Co-authored-by: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com>
…le is limited to the screen width on mobile devices, add an option to control whether the table is restricted to div
Comment on lines 51 to 52
table_open: require('./MdToHtml/rules/table_open').default,
table_close: require('./MdToHtml/rules/table_close').default,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these two functions work together, could you put them together in the same file and use a meaningful name please? The basic structure would be like this:

export default {
	plugin: (markdownIt: any, params: any) => {
		markdownIt.renderer.rules.table_close = function(tokens: any[], idx: number, options: any, _env: any, self: any) {
			// ...			
		};

		markdownIt.renderer.rules.table_open = function(tokens: any[], idx: number, options: any, _env: any, self: any) {
			// ...
		};
	},
};

What does the plugin do exactly? It makes the table horizontally scrollable on mobile, is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this plugin makes the table horizontally scrollable on mobile, so the maximum width of the table is the screen width. When dragging notes down, the interface does not sway left or right.

<div class="joplin-table-div" >
      <table class="maps-to-line" source-line="186" source-line-end="312">
     .....
      </table>
</div>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laurent22 I didn't have a deep understanding of Joplin before, and under your guidance, I have re implemented it by adding options. And I found that makes the table horizontally scrollable on desktop platforms, operating experience is also good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export default {
	plugin: (markdownIt: any, params: any) => {
		markdownIt.renderer.rules.table_close = function(tokens: any[], idx: number, options: any, _env: any, self: any) {
			// ...			
		};

		markdownIt.renderer.rules.table_open = function(tokens: any[], idx: number, options: any, _env: any, self: any) {
			// ...
		};
	},
};

Rebuild to: packages/renderer/MdToHtml/rules/tableHorizontallyScrollable.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you agree with the option name "tableHorizontalyScrollable"? If you agree, I will try to commit modifications of
introduction options(readme/apps/markdown.md) and translations file(packages/tools/locales)

wljince007

This comment was marked as duplicate.

@wljince007 wljince007 changed the title Mobile: Table adds div parent on mobile, so the width of the table is limited to the screen width on mobile Mobile, Desktop: Make table horizontally scrollable by give table a div parent, so the width of the table is limited to the screen width. Mar 21, 2024
@laurent22
Copy link
Owner

Thanks for taking the time to implement this @wljince007. Just two things:

  • Is it possible to add tests for it? Have a look at packages/app-cli/tests/MdToHtml.ts, I think you can just add a test case for your change.

  • Why is there this option markdown.plugin.tableHorizontallyScrollable? I'm thinking everybody would benefit from this change, so there's no need to make it optional?

@wljince007
Copy link
Contributor Author

wljince007 commented Mar 23, 2024

@laurent22
Added test: packages/app-cli/tests/md_to_html/{table_horizontally_scrollable_1.md, table_horizontally_scrollable_2.md}, test runs ok.

At that time, I wanted to distinguish the use of floating scrollbars when loading plugins on the desktop, but now it seems unnecessary to add an option, already remove this option.

Comment on lines 4 to 10
if (tokens[idx].map) {
const line = tokens[idx].map[0];
const lineEnd = tokens[idx].map[1];
tokens[idx].attrJoin('class', 'maps-to-line');
tokens[idx].attrSet('source-line', `${line}`);
tokens[idx].attrSet('source-line-end', `${lineEnd}`);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy from packages/renderer/MdToHtml/rules/source_map.ts, I will remove it.

@@ -0,0 +1,19 @@
export default {
plugin: (markdownIt: any) => {
markdownIt.renderer.rules.table_open = function(tokens: any[], idx: number, options: any, _env: any, self: any) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use any for any of the variables that you use. So tokens, options and self should have a type. You can get it from the markdown-it package

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is copy from packages/renderer/MdToHtml/rules/source_map.ts.
The exact type of self variable is Renderer, but type Renderer has not been exported by markdown-it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to

markdownIt.renderer.rules.table_open = function(tokens: MarkdownIt.Token[], idx: number, options: MarkdownIt.Options, _env: any, renderer: any) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@laurent22 Need I refactoring packages/renderer/MdToHtml/rules/{checkbox.ts, code_inline.ts, fence.ts, fountain.ts, highlight_keywords.ts, html_image.ts, image.ts, katex.ts, link_close.ts, link_open.ts, mermaid.ts, sanitize_html.ts, source_map.ts} to add tokens, options types?

tokens[idx].attrSet('source-line', `${line}`);
tokens[idx].attrSet('source-line-end', `${lineEnd}`);
}
const cur = String(self.renderToken(tokens, idx, options));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the return type of self.renderToken() - isn't it already a string?

Also please avoid abbreviations, so cur => current

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, redundant operations will be removed.

Comment on lines 14 to 17
markdownIt.renderer.rules.table_close = function(tokens: any[], idx: number, options: any, _env: any, self: any) {
const cur = String(self.renderToken(tokens, idx, options));
return `${cur}</div>\n`;
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

@@ -434,6 +434,11 @@ export default function(theme: any, options: Options = null) {
overflow-x: auto;
}

.joplin-table-div{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name it joplin-table-wrapper please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

@laurent22
Copy link
Owner

It looks good now, thank you @wljince007!

@laurent22 laurent22 merged commit 2de5c1b into laurent22:dev Mar 25, 2024
10 checks passed
@wljince007 wljince007 deleted the table_add_div_on_mobile_only branch March 27, 2024 02:53
@wljince007 wljince007 restored the table_add_div_on_mobile_only branch March 27, 2024 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants