Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/extension-smalruby-console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Scratch には「言う」「聞いて待つ」しかないが、日本語のプ
`showSmalrubyConsole(dispatch)` / `hideSmalrubyConsole(dispatch)` を呼ぶ)。人が隠したこと(ボタン・
[ステージにもどる])は Redux の `hiddenByUser` に残り、`showSmalrubyConsole` はそれを上書きしない
(コードタブから戻っても隠したまま)。`hideSmalrubyConsole`(ルビータブ)はこれを忘れる。
- 日本語モードのあいだ(最後に開いた文字のタブが日本語タブ)は、ルビータブ以外のどのタブでもコンソールを既定にする
(`shouldShowSmalrubyConsoleByDefault`)。そのため、コードタブのままブラウザを読み込み直しても、コンソールが出る。
`hiddenByUser` は作品に保存しないので、読み込み直すと、人が隠していたコンソールもまた出る。
日本語モードを許さないレッスンでは出さない(日本語モードのコードタブでそのレッスンを始めたときも、ステージに戻す)。
- 日本語タブを開いても、出すのは**コンソールの画面だけ**で、拡張機能は作品に足さない。拡張機能は、
日本語 → Ruby の変換で `表示する` / `【外部からの入力】`(Ruby の `console.puts` / `console.gets`)が
出て、そのブロックを作るときに初めて足す(変換器がブロックの opcode から拡張機能を読み込む)。
Expand Down
28 changes: 19 additions & 9 deletions packages/scratch-gui/src/containers/ruby-tab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ import { generatePreviewCode } from '../lib/ruby-script-preview';
import { targetCodeToBlocks } from '../lib/ruby-to-blocks-converter';
import RubyToBlocksConverterHOC from '../lib/ruby-to-blocks-converter-hoc.jsx';
import { containsV1Code } from '../lib/ruby-to-blocks-converter/v1-detection';
import { hideSmalrubyConsole, showSmalrubyConsole } from '../lib/smalruby-console.js';
import {
hideSmalrubyConsole,
shouldShowSmalrubyConsoleByDefault,
showSmalrubyConsole,
} from '../lib/smalruby-console.js';
import { getUrlParams } from '../lib/url-params';
import { showAlertWithTimeout, closeAlertWithId } from '../reducers/alerts';
import {
Expand Down Expand Up @@ -1613,18 +1617,24 @@ const RubyTab = (props) => {
if (exitDnclModeExternallyRequested) onClearExitDnclModeRequest?.();
}, [exitDnclModeExternallyRequested, dnclModeLocked, locale, activeTabIndex]);

// The console is the stage of the Japanese tab (docs/dncl/design.md 5 章): it is shown while the
// Japanese tab shows the program in Japanese, and the Ruby tab gives the stage back. Only the
// screen is shown: the console extension is added by the conversion when the program prints
// or reads (`表示する` / `入力する`). The editing target is a dependency so that the console
// comes back after a project is loaded.
const japaneseTabActive = activeTabIndex === JAPANESE_TAB_INDEX;
// The console is the stage of the Japanese mode (docs/dncl/design.md 5 章): it is shown while the
// program is written in Japanese, in the Japanese tab and in the other tabs (a reload in the Code
// tab shows it too, B31), and the Ruby tab gives the stage back. Only the screen is shown: the
// console extension is added by the conversion when the program prints or reads
// (`表示する` / `入力する`). The editing target is a dependency so that the console comes back
// after a project is loaded.
const showsConsoleByDefault = shouldShowSmalrubyConsoleByDefault({ dnclMode, dnclModeLocked, activeTabIndex });
useEffect(() => {
if (dnclMode && japaneseTabActive) onShowSmalrubyConsole?.();
}, [dnclMode, japaneseTabActive, editingTarget]);
if (showsConsoleByDefault) onShowSmalrubyConsole?.();
}, [showsConsoleByDefault, editingTarget]);
useEffect(() => {
if (activeTabIndex === RUBY_TAB_INDEX) onHideSmalrubyConsole?.();
}, [activeTabIndex]);
// A lesson that does not allow Japanese gives the stage back too, even when it starts in the
// Code tab of the Japanese mode (the console shown there would hide the stage of the lesson).
useEffect(() => {
if (dnclModeLocked && dnclModeRef.current) onHideSmalrubyConsole?.();
}, [dnclModeLocked]);

// Furigana toggle effect
useEffect(() => {
Expand Down
23 changes: 22 additions & 1 deletion packages/scratch-gui/src/lib/smalruby-console.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// The entry the other parts of the GUI (the Japanese tab) use to show the
// console of the console extension (docs/extension-smalruby-console/README.md).
import { RUBY_TAB_INDEX } from '../reducers/editor-tab.js';
import { resetSmalrubyConsole, showSmalrubyConsoleByDefault } from '../reducers/smalruby-console.js';
import { getStageDimensions } from './screen-utils.js';

Expand All @@ -18,6 +19,20 @@ const showSmalrubyConsole = (dispatch) => {
dispatch(showSmalrubyConsoleByDefault());
};

/**
* Whether the console should be shown (unless the user hid it). The console is the stage
* of the Japanese mode: the Japanese tab shows it, and the other tabs keep it, except the
* Ruby tab, which gives the stage back. So a reload in the Code tab of the Japanese mode
* shows the console too (B31).
* @param {object} state - The state of the editor.
* @param {boolean} state.dnclMode - Whether the program is written in Japanese.
* @param {boolean} [state.dnclModeLocked] - Whether a lesson does not allow the Japanese mode.
* @param {number} state.activeTabIndex - The active tab.
* @returns {boolean} Whether to show the console.
*/
const shouldShowSmalrubyConsoleByDefault = ({ dnclMode, dnclModeLocked = false, activeTabIndex }) =>
Boolean(dnclMode) && !dnclModeLocked && activeTabIndex !== RUBY_TAB_INDEX;

/**
* Show the stage again in place of the console (the Ruby tab). The next Japanese tab
* shows the console again, even if the user hid it.
Expand All @@ -41,4 +56,10 @@ const stageCanvasSize = (rect, stageSize) => {
return { width, height };
};

export { SMALRUBY_CONSOLE_EXTENSION_ID, hideSmalrubyConsole, showSmalrubyConsole, stageCanvasSize };
export {
SMALRUBY_CONSOLE_EXTENSION_ID,
hideSmalrubyConsole,
shouldShowSmalrubyConsoleByDefault,
showSmalrubyConsole,
stageCanvasSize,
};
45 changes: 44 additions & 1 deletion packages/scratch-gui/test/unit/lib/smalruby-console.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { getStageDimensions } from '../../../src/lib/screen-utils';
import { hideSmalrubyConsole, showSmalrubyConsole, stageCanvasSize } from '../../../src/lib/smalruby-console';
import {
hideSmalrubyConsole,
shouldShowSmalrubyConsoleByDefault,
showSmalrubyConsole,
stageCanvasSize,
} from '../../../src/lib/smalruby-console';
import {
BLOCKS_TAB_INDEX,
COSTUMES_TAB_INDEX,
JAPANESE_TAB_INDEX,
RUBY_TAB_INDEX,
} from '../../../src/reducers/editor-tab';
import { resetSmalrubyConsole, showSmalrubyConsoleByDefault } from '../../../src/reducers/smalruby-console';

describe('showSmalrubyConsole', () => {
Expand All @@ -14,6 +25,38 @@ describe('showSmalrubyConsole', () => {
});
});

describe('shouldShowSmalrubyConsoleByDefault', () => {
test('shows the console in the Japanese tab of the Japanese mode', () => {
expect(shouldShowSmalrubyConsoleByDefault({ dnclMode: true, activeTabIndex: JAPANESE_TAB_INDEX })).toBe(true);
});

test('shows the console in the other tabs of the Japanese mode (a reload in the Code tab, B31)', () => {
expect(shouldShowSmalrubyConsoleByDefault({ dnclMode: true, activeTabIndex: BLOCKS_TAB_INDEX })).toBe(true);
expect(shouldShowSmalrubyConsoleByDefault({ dnclMode: true, activeTabIndex: COSTUMES_TAB_INDEX })).toBe(true);
});

test('leaves the stage in the Ruby tab', () => {
expect(shouldShowSmalrubyConsoleByDefault({ dnclMode: true, activeTabIndex: RUBY_TAB_INDEX })).toBe(false);
});

test('leaves the stage out of the Japanese mode', () => {
expect(shouldShowSmalrubyConsoleByDefault({ dnclMode: false, activeTabIndex: BLOCKS_TAB_INDEX })).toBe(false);
expect(shouldShowSmalrubyConsoleByDefault({ dnclMode: false, activeTabIndex: JAPANESE_TAB_INDEX })).toBe(
false,
);
});

test('leaves the stage in a lesson that does not allow the Japanese mode', () => {
expect(
shouldShowSmalrubyConsoleByDefault({
dnclMode: true,
dnclModeLocked: true,
activeTabIndex: BLOCKS_TAB_INDEX,
}),
).toBe(false);
});
});

describe('hideSmalrubyConsole', () => {
test('shows the stage again, and forgets that the user hid the console', () => {
const dispatch = jest.fn();
Expand Down
Loading